Introduction to the Binary Number System
At the heart of every modern computing device sits a simple, elegant system of zeros and ones: the binary number system (or base-2). While humans have historically preferred decimal notation (base-10)—likely due to having ten fingers—computers are built on physical electrical pathways that can either be "on" or "off."
In this guide, we will break down binary arithmetic, look at bitwise logical gates, explore computer architecture and data representation, and see how to convert between base systems (decimal, binary, hexadecimal, and octal). Whether you are an undergraduate computer science student, a developer writing low-level firmware, a networking engineer configuring subnets, or a cybersecurity researcher analyzing buffer overflows, a deep understanding of binary numbers is essential.
The Mathematics of Base-2
A positional number system represents numbers by placing digits in sequential columns. Each column corresponds to a multiplier base raised to an exponent.
In the decimal system (base-10), the columns from right to left represent units ($10^0 = 1$), tens ($10^1 = 10$), hundreds ($10^2 = 100$), and so forth.
In the binary system (base-2), the columns represent powers of two:
- $2^0$ = 1 (Least Significant Bit - LSB)
- $2^1$ = 2
- $2^2$ = 4
- $2^3$ = 8
- $2^4$ = 16
- $2^5$ = 32
- $2^6$ = 64
- $2^7$ = 128 (Most Significant Bit - MSB in a single byte)
To represent the decimal value 13 in binary, we decompose it into powers of two:
$$13 = 8 + 4 + 1 = (1 \times 2^3) + (1 \times 2^2) + (0 \times 2^1) + (1 \times 2^0)$$
Written sequentially, this gives us 1101 in binary.
Binary Arithmetic Operations
Performing arithmetic in binary follows the exact same logic as decimal math, but your carry and borrow thresholds are triggered at 2 instead of 10.
1. Binary Addition
The four fundamental rules of binary addition are:
- $0 + 0 = 0$
- $0 + 1 = 1$
- $1 + 0 = 1$
- $1 + 1 = 10$ (write
0, carry1) - $1 + 1 + 1$ (with carry-in) $= 11$ (write
1, carry1)
Example: Adding $1101_2$ (13) and $0110_2$ (6):
Carry: 1100
1101 (13)
+ 0110 (6)
------
10011 (19)
2. Binary Subtraction
Binary subtraction relies on "borrowing" from the next column. When borrowing, you borrow a value of $2$ ($10_2$):
- $0 - 0 = 0$
- $1 - 0 = 1$
- $1 - 1 = 0$
- $0 - 1 = 1$ (borrow $1$ from the next active column, turning the $0$ into $2$)
In modern CPUs, subtraction is actually performed using addition. The minuend is added to the two's complement representation of the subtrahend. This eliminates the need for separate subtraction circuits in hardware.
3. Binary Multiplication
Binary multiplication is simpler than decimal multiplication because you only multiply by 0 or 1.
- $0 \times 0 = 0$
- $0 \times 1 = 0$
- $1 \times 0 = 0$
- $1 \times 1 = 1$
You perform standard partial products and shift them, then add the columns. Example: $101_2 \times 011_2$ (5 $\times$ 3):
101
× 011
-----
101 (101 × 1)
1010 (101 × 1, shifted left)
+ 00000 (101 × 0, shifted left twice)
-------
1111 (15 in decimal)
4. Binary Division
Binary division uses long division rules. You check if the divisor fits into the current portion of the dividend. If yes, write 1 in the quotient, subtract, pull down the next digit, and repeat. If no, write 0, pull down the next digit, and repeat.
Data Representation: Signed vs. Unsigned
How a CPU interprets a sequence of bits depends on the data type definition.
Unsigned Integers
In unsigned mode, every bit is used to store positive numerical magnitude. In an 8-bit byte, the values range from $00000000_2$ (0) to $11111111_2$ ($128+64+32+16+8+4+2+1 = 255$). The formula for the maximum range is $2^n - 1$, where $n$ is the word size in bits.
Signed Integers (Two's Complement)
To represent negative numbers, computer science uses Two's Complement. In this scheme, the most significant bit (MSB) acts as the sign bit. If MSB is 0, the number is positive. If MSB is 1, the number is negative.
To find the Two's Complement representation of $-5$ in an 8-bit space:
- Represent $+5$ in binary:
00000101 - Invert all bits (One's Complement):
11111010 - Add 1 to the LSB:
11111011
Thus, 11111011 represents $-5$. When we add $+5$ (00000101) and $-5$ (11111011), the arithmetic naturally overflows back to 00000000 (ignoring the final carry-out bit), proving the mathematical consistency of this system.
Bitwise Logical Operations
Bitwise operations perform boolean logic on a bit-by-bit basis between two binary inputs. These operations are executed in a single CPU clock cycle, making them fast and highly efficient.
| Gate | Name | Rule | Truth Table Example |
| :--- | :--- | :--- | :--- |
| AND | Conjunction | Output is 1 only if both inputs are 1. | 1 AND 1 = 1, 1 AND 0 = 0 |
| OR | Disjunction | Output is 1 if at least one input is 1. | 1 OR 0 = 1, 0 OR 0 = 0 |
| XOR | Exclusive OR | Output is 1 if inputs are different. | 1 XOR 0 = 1, 1 XOR 1 = 0 |
| NOT | Inverter | Inverts the input bit. | NOT 1 = 0, NOT 0 = 1 |
| NAND | Not AND | Output is 0 only if both inputs are 1. | 1 NAND 1 = 0, 1 NAND 0 = 1 |
| NOR | Not OR | Output is 1 only if both inputs are 0. | 1 NOR 0 = 0, 0 NOR 0 = 1 |
| XNOR | Equivalence | Output is 1 if inputs are identical. | 1 XNOR 1 = 1, 1 XNOR 0 = 0 |
Bitwise Shifting
- Left Shift (
<<): Moves all bits left by a specified number of positions, discarding the leftmost bits and padding the right with zeros. This acts as a rapid multiplication by powers of 2. - Right Shift (
>>): Moves all bits right.- Logical Right Shift: Pads the left side with zeros (used for unsigned numbers).
- Arithmetic Right Shift: Pads the left side with copies of the sign bit to preserve negative values (used for signed integers).
Number System Conversions
Programmers often convert between binary, octal, decimal, and hexadecimal.
Binary to Hexadecimal
Hexadecimal (base-16) uses numbers 0-9 and letters A-F to represent values $10$ to $15$. Since $16 = 2^4$, exactly 4 binary bits (a nibble) correspond to 1 hexadecimal character:
0000= 01001= 91010= A1111= F
To convert 11010111 to hex, split it into two nibbles:
1101= 13 (HexD)0111= 7 (Hex7) So11010111in binary isD7in hexadecimal.
Binary to Octal
Octal (base-8) uses digits 0-7. Since $8 = 2^3$, exactly 3 binary bits correspond to 1 octal character. Split the binary number into groups of 3 starting from the right.
Floating Point Binary Numbers (IEEE 754)
Not all numbers are integers; real-world math requires decimals/fractions. In binary, fractional representation uses a decimal point (radix point) where columns to the right represent negative powers of two: $$2^{-1} = 0.5$$ $$2^{-2} = 0.25$$ $$2^{-3} = 0.125$$
To represent very large or very small numbers, computers use the IEEE 754 Floating-Point Standard. In single-precision format (32-bit float):
- Sign Bit (1 bit):
0for positive,1for negative. - Exponent (8 bits): Biased by 127 to allow positive and negative exponents.
- Mantissa / Fraction (23 bits): Represents the precision bits of the normalized number.
This structured allocation allows computers to handle massive scientific ranges while maintaining uniform memory layouts.
Practical Applications
1. Software Development
In systems programming (like C, C++, Rust), developers use bitwise operations for packing data, writing device drivers, parsing network headers, and implementing memory-mapped I/O.
2. Networking
IP addresses are processed by hardware as 32-bit binary strings. The Subnet Mask is used in a bitwise AND operation with the IP Address to isolate the Network Address from the Host Address.
3. Cybersecurity & Cryptography
Ciphers (like AES or ChaCha20) make heavy use of XOR operations because XORing a message with a key encrypts it, and XORing it again with the same key decrypts it. Security practitioners also inspect raw binary payloads (shellcode) to debug security exploits.