1. Binary Integers
How computers count with ones and zeros
Every number you type into a computer — your age, a pixel color, a file size — ends up stored as a sequence of ones and zeros. This isn't just an implementation detail. It shapes what numbers computers can represent, how arithmetic works at the hardware level, and why mysterious things happen when values get "too large." Understanding binary integers is the foundation for everything else in this series.
Positional Notation
You already understand positional notation — you use it every time you read a number. In base 10 (decimal), each digit's contribution depends on its position:
Decimal 4,209
4 × 1000 + 2 × 100 + 0 × 10 + 9 × 1
= 4 × 10³ + 2 × 10² + 0 × 10¹ + 9 × 10⁰
= 4000 + 200 + 0 + 9
= 4,209
Each column is a power of 10. The rightmost column is 10⁰ = 1, the next is 10¹ = 10, and so on.
Base 2 (binary) works identically — but with powers of 2 instead of powers of 10, and only two possible digits: 0 and 1.
Why do computers use base 2? Because the underlying hardware consists of transistors that are either conducting (on) or not conducting (off). Two states, two digits. Everything else follows from that physical reality.
A Simple Example
Let's convert decimal 42 to binary step by step using repeated division.
Converting 42 to binary: repeated division by 2
42 ÷ 2 = 21 remainder 0 ← least significant bit
21 ÷ 2 = 10 remainder 1
10 ÷ 2 = 5 remainder 0
5 ÷ 2 = 2 remainder 1
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1 ← most significant bit
Reading remainders from bottom to top: 101010
To verify, expand the binary result using place values:
Checking: 101010₂
1 × 32 + 0 × 16 + 1 × 8 + 0 × 4 + 1 × 2 + 0 × 1
= 32 + 0 + 8 + 0 + 2 + 0
= 42 ✓
Bit Positions and Place Values
In an 8-bit number, each bit has a position numbered 7 down to 0 (right to left), with a corresponding place value that doubles each step.
The key insight: each bit is either "on" (1, contributing its place value to the total) or "off" (0, contributing nothing). The value of a binary number is simply the sum of the place values for all the "on" bits.
Unsigned Integers
An unsigned integer uses all its bits to represent non-negative numbers — no negatives, no fractions.
With n bits you can represent 2ⁿ distinct values, from 0 up to 2ⁿ − 1.
| Bit width | Min | Max | Common use |
|---|---|---|---|
| 8 bits | 0 | 255 | Byte, pixel channel value |
| 16 bits | 0 | 65,535 | Port numbers, small counters |
| 32 bits | 0 | 4,294,967,295 (~4.3 billion) | Array indices, IPv4 addresses |
| 64 bits | 0 | ~1.8 × 10¹⁹ | File sizes, timestamps |
You can verify the maxima with the formula 2ⁿ − 1. An 8-bit number maxes out at 2⁸ − 1 = 255. A 32-bit number maxes out at 2³² − 1 = 4,294,967,295 — which is why early 32-bit operating systems couldn't address more than 4 GB of RAM.
Signed Integers
Most real programs need negative numbers. Several approaches have been tried.
Sign-magnitude is the intuitive approach: dedicate one bit (the most significant bit) as a sign bit (0 = positive, 1 = negative), and use the remaining bits for the magnitude. The problem: this gives you two representations of zero (00000000 = +0 and 10000000 = −0), and arithmetic circuits need special-case logic to handle the sign bit separately.
Ones' complement negates a number by flipping all its bits. So +42 is 00101010 and −42 is 11010101. This eliminates most arithmetic special-cases, but still suffers from two zeros: 00000000 (+0) and 11111111 (−0). Ones' complement was used in some early computers but is essentially obsolete today.
Two's complement is what every modern processor uses.
To negate a number in two's complement:
- Flip all the bits (ones' complement)
- Add 1
Negating 42 in 8-bit two's complement
+42 = 00101010
Step 1 — flip all bits:
11010101
Step 2 — add 1:
11010101
+ 1
----------
11010110 = −42
To verify: 11010110 → 00101001 + 1 = 00101010 = 42. The round trip works.
In two's complement with n bits, the range is −2^(n−1) to 2^(n−1) − 1. For 8 bits: −128 to +127. For 32 bits: −2,147,483,648 to +2,147,483,647.
Why Two's Complement Wins
Two's complement has two decisive advantages over its alternatives.
Single zero. There is exactly one bit pattern for zero: all zeros. This simplifies comparisons and eliminates the anomaly of −0.
Addition just works. You can add any two two's complement numbers with the same circuit you use for unsigned addition. No special handling for the sign bit.
Adding +5 and −3 (8-bit two's complement)
+5 = 00000101
−3 = 11111101
─────────────
00000010 (carry out of bit 7 is discarded)
= +2 ✓
The hardware just adds and ignores the carry out of the top bit. No sign-aware logic needed.
The circular number line below shows why this works geometrically. In a 4-bit two's complement system, counting past the maximum positive value (+7) wraps directly to the most negative value (−8) — and counting below −8 wraps back to +7.
Overflow and Wraparound
Overflow occurs when an arithmetic result exceeds the representable range and the bit pattern "wraps around" to the other end.
For a signed 8-bit integer, the range is −128 to +127. Adding 1 to 127 overflows:
Signed 8-bit overflow: 127 + 1
127 = 01111111
+ 1 = 00000001
─────────────
10000000 = −128 (signed interpretation)
The carry ripples through all seven magnitude bits and sets the sign bit. The result looks negative.
The same wrapping happens in the other direction: subtracting 1 from −128 gives +127.
For unsigned integers, overflow wraps from the maximum value back to zero:
Unsigned 8-bit overflow: 255 + 1
255 = 11111111
+ 1 = 00000001
─────────────
(1) 00000000 = 0 (carry out is discarded)
In languages like C and C++, signed integer overflow is technically undefined behavior — the compiler is allowed to assume it never happens and optimize accordingly, which can produce surprising results. Unsigned overflow, however, is well-defined and always wraps. In languages like Rust, debug builds will panic on overflow; release builds wrap silently.
Critical Insight
A sequence of bits has no intrinsic meaning. The value 11111111 is
255 if you interpret it as unsigned, and −1 if you interpret
it as signed 8-bit two's complement. The hardware stores bits; the type system decides what those
bits mean.
Key Takeaways
- Binary is positional notation in base 2 — each bit is a power of 2
- Unsigned integers use all bits for magnitude; the range is 0 to 2ⁿ − 1
- Signed integers use two's complement: negate by flipping all bits and adding 1
- Two's complement has exactly one zero and lets addition work without sign-aware hardware
- Overflow wraps the value around; unsigned wrapping is well-defined, signed overflow is undefined in C/C++
- Bits have no intrinsic sign — interpretation is determined by the declared type
Try It Yourself — Binary Integer Converter
Enter any integer below to see its binary representation, signed and unsigned interpretations, and two's complement conversion for negative numbers.