Binary Arithmetic
Part of Basic Computing
Binary arithmetic is the mathematical foundation of all digital computing — performing addition, subtraction, multiplication, and division using only 0s and 1s.
Why This Matters
Every calculation performed by every computer in history happens in binary. The processor does not think in decimal; it manipulates patterns of two voltages. Understanding binary arithmetic means understanding the actual operations the hardware performs, rather than relying on abstract API layers.
This knowledge becomes critical when building computing systems from scratch. Designing adder circuits, writing low-level code, debugging hardware failures, or implementing arithmetic in restricted environments all require fluency with binary. A person who can only do decimal arithmetic is dependent on pre-existing calculators; one who understands binary can build and reason about calculation from first principles.
The rules of binary arithmetic are actually simpler than decimal — only two symbols instead of ten, with correspondingly simpler carry and borrow rules. Mastery requires practice but not exceptional mathematical ability.
Binary Addition
Binary addition follows four simple rules:
- 0 + 0 = 0 (no carry)
- 0 + 1 = 1 (no carry)
- 1 + 0 = 1 (no carry)
- 1 + 1 = 0, carry 1
Adding multi-bit numbers proceeds column by column from right (least significant) to left (most significant), exactly like decimal addition.
Example: 0110 (6) + 0101 (5)
0110
+ 0101
------
Column 0 (rightmost): 0 + 1 = 1, carry 0 Column 1: 1 + 0 = 1, carry 0 Column 2: 1 + 1 = 0, carry 1 Column 3: 0 + 0 + 1(carry) = 1, carry 0
Result: 1011 (11). Correct.
When the result exceeds the available bits, a carry out occurs — this is the hardware carry flag.
Example: 1100 (12) + 0101 (5) in 4 bits:
1100
+ 0101
------
0001 (carry out = 1, true result = 17 but only 4 bits stored)
The carry flag signals this overflow condition. Programs must check the carry flag after addition to handle numbers larger than one register width.
Binary Subtraction and Two’s Complement
Subtraction can be performed directly using borrow rules analogous to decimal, but hardware almost universally uses two’s complement instead — it converts subtraction into addition, requiring only one circuit type.
Two’s complement negation: to negate a number, invert all bits then add 1.
Example: negate 0110 (6)
- Invert: 1001
- Add 1: 1010
- Result: 1010, which represents −6 in two’s complement
Subtraction via addition: A − B = A + (−B) = A + (NOT B) + 1
Example: 0111 (7) − 0011 (3):
- NOT 0011 = 1100
- 0111 + 1100 + 1 = 0111 + 1101 = 10100, drop carry = 0100 (4). Correct.
Range of two’s complement N-bit numbers: −2^(N−1) to +2^(N−1)−1
- 4-bit: −8 to +7
- 8-bit: −128 to +127
- 16-bit: −32768 to +32767
The most significant bit (MSB) is the sign bit: 0 = positive, 1 = negative.
Overflow detection: signed overflow occurs when adding two numbers of the same sign produces a result of opposite sign. In hardware, overflow = carry into MSB XOR carry out of MSB.
Binary Multiplication
The standard algorithm mirrors decimal long multiplication, using binary rules.
Multiply 0110 (6) × 0101 (5):
0110
× 0101
------
0110 (0110 × 1, shift 0)
0000 (0110 × 0, shift 1)
0110 (0110 × 1, shift 2)
0000 (0110 × 0, shift 3)
--------
0011110 = 30 ✓
The rule: for each bit of the multiplier (right to left), if the bit is 1, add the multiplicand shifted left by the bit position; if 0, add zero. Sum all partial products.
Hardware multiplier circuits do this with shift registers and adders. For N-bit inputs, the result is 2N bits wide. An 8×8 multiplier produces 16-bit results; a 32×32 produces 64-bit results.
A simple software multiplier using only addition and shifting:
result = 0
for each bit position i (0 to N-1):
if multiplier bit i is 1:
result += multiplicand << i
return result
Repeated doubling (shift left = multiply by 2) combined with conditional addition is the core mechanism.
Binary Division
Binary division mirrors long division. The quotient is built bit by bit, and the remainder shrinks at each step.
Divide 1010 (10) by 0011 (3):
1010 ÷ 0011
- Shift dividend left by (N−1) positions, compare with divisor
- Working through: 10 ÷ 3 = 3 remainder 1
The restoring division algorithm:
- Initialize remainder R = 0, quotient Q = 0
- For each bit of dividend (MSB first):
- Shift R left 1, bring in next dividend bit as LSB
- R = R − divisor
- If R ≥ 0: quotient bit = 1, keep R
- If R < 0: quotient bit = 0, restore R = R + divisor
- Repeat for all N bits
Division by powers of 2 is simply right-shift: A ÷ 2 = A >> 1 (with the shifted-out bit as the remainder). This is why division by 2 is cheap and division by arbitrary numbers is expensive in hardware.
Bitwise Operations and Arithmetic Tricks
Beyond the four arithmetic operations, several bitwise operations are arithmetically useful:
Shift left by n: equivalent to multiply by 2^n (fast, single-cycle operation)
Shift right by n: equivalent to divide by 2^n (unsigned), or arithmetic divide (signed)
AND with mask: extract specific bits; value AND 0x0F extracts the low nibble
OR with mask: set specific bits; value OR 0x80 sets the MSB
XOR with mask: toggle specific bits; value XOR 0xFF inverts all bits (bitwise NOT)
Useful arithmetic identities:
- Test if even:
value AND 1 == 0 - Absolute value: complicated in general but fast using sign bit tricks
- Modulo power-of-2:
value AND (2^n − 1)(e.g., mod 8 =AND 0x07) - Round up to power-of-2: standard bit-manipulation trick using leading zero count
These tricks appear constantly in systems programming, hardware description, and protocol implementation. Familiarity with them is a mark of real computational fluency.