Positional notation and number bases
Every number system we use is positional — the value of a digit depends on its position. In decimal (base 10), the digit 3 in "305" means 300 (3 × 10²), whereas the 3 in "53" means 3 (3 × 10⁰).
The base determines how many distinct digits exist. Decimal has 10 (0–9). Binary has 2 (0 and 1). Hexadecimal has 16 (0–9 and A–F). Octal has 8 (0–7).
Why computers use binary
Transistors have two states: on and off. A single transistor reliably represents one binary digit (bit). Building circuitry for ten states (decimal) would require far more complex switching logic and be far less noise-tolerant.
Everything a computer does — addition, multiplication, storage, network transmission — is ultimately binary arithmetic on groups of 8 bits (bytes) or multiples thereof.
Hexadecimal: the developer's shorthand
Binary is awkward to write for humans — 255 is 11111111. Hex is a convenient shorthand: each hex digit maps exactly to 4 binary bits (a nibble), so FF = 1111 1111. This bijection makes hex-binary conversion mental arithmetic.
Hex is everywhere in computing:
- Colors:
#EE7A2B= red 238, green 122, blue 43 - Memory addresses:
0x7fff5fbff5a8 - Crypto hashes: SHA-256 outputs 64 hex characters (32 bytes)
- Byte representations:
\xDE\xAD\xBE\xEF - Unicode code points: U+0041 is 'A'
Octal and Unix permissions
Octal predates hex in many Unix tools because it maps cleanly to 3-bit groups. Unix file permissions are 9 bits: 3 bits each for owner, group, and others. Each group of 3 bits represents a single octal digit:
chmod 755 = rwxr-xr-x
7 = 111 (rwx)
5 = 101 (r-x)
5 = 101 (r-x)
Bit widths and data types
The bit length of a number determines which integer type can store it:
| Bits | Unsigned range | Common type |
|---|---|---|
| 8 | 0 – 255 | uint8, byte |
| 16 | 0 – 65,535 | uint16 |
| 32 | 0 – 4,294,967,295 | uint32, unsigned int |
| 64 | 0 – 18.4 × 10¹⁸ | uint64, ulong |
When you see an unexpected negative number or a value wrapping around to zero, integer overflow is likely: the value exceeded the maximum for its type and wrapped around.
Converting between bases by hand
To convert from any base to decimal: multiply each digit by its base raised to its position power and sum the results.
Binary 1011 = 1×2³ + 0×2² + 1×2¹ + 1×2⁰ = 8 + 0 + 2 + 1 = 11.
To convert decimal to binary: repeatedly divide by 2 and read the remainders from bottom to top. 11 ÷ 2 = 5 r 1, 5 ÷ 2 = 2 r 1, 2 ÷ 2 = 1 r 0, 1 ÷ 2 = 0 r 1 → 1011.