Binary & Number Systems
Binary, hexadecimal and octal; converting between bases; two's complement for negative numbers; and overflow.
Computers use binary (base 2) because a transistor has two reliable states, off and on. Everything else — integers, text, images, programs — is built on top of patterns of 0s and 1s. You don't have to compute in binary all day, but you need to be comfortable reading it.
Positional number systems
In any base b, each digit is worth the base raised to its position:
| Base | Name | Digits | Prefix in code | Example (decimal 255) |
|---|---|---|---|---|
| 2 | Binary | 0–1 | 0b | 0b11111111 |
| 8 | Octal | 0–7 | 0o / leading 0 | 0o377 |
| 10 | Decimal | 0–9 | — | 255 |
| 16 | Hexadecimal | 0–9, A–F | 0x | 0xFF |
Why hexadecimal?
One hex digit is exactly 4 bits (a nibble), so two hex digits are one byte. Hex is a compact, human-friendly way to write binary: memory addresses (0x7ffe…), colours (#FF8800), hashes and byte dumps all use it.
| Hex | Binary | Hex | Binary |
|---|---|---|---|
| 0 | 0000 | 8 | 1000 |
| 1 | 0001 | 9 | 1001 |
| 2 | 0010 | A | 1010 |
| 3 | 0011 | B | 1011 |
| 4 | 0100 | C | 1100 |
| 5 | 0101 | D | 1101 |
| 6 | 0110 | E | 1110 |
| 7 | 0111 | F | 1111 |
To convert binary ↔ hex, group bits in fours from the right: 1101 0110 = D6.
Converting decimal to binary
Repeatedly divide by 2 and read the remainders from bottom to top.
- 13 ÷ 2 = 6 remainder 1
- 6 ÷ 2 = 3 remainder 0
- 3 ÷ 2 = 1 remainder 1
- 1 ÷ 2 = 0 remainder 1 → read upwards: 1101
Integer.toBinaryString(13); // "1101"
Integer.toHexString(255); // "ff"
Integer.parseInt("1101", 2); // 13
Integer.parseInt("ff", 16); // 255bin(13) # '0b1101'
hex(255) # '0xff'
int("1101", 2) # 13
int("ff", 16) # 255
f"{13:08b}" # '00001101' (padded)(13).toString(2); // "1101"
(255).toString(16); // "ff"
parseInt("1101", 2); // 13
parseInt("ff", 16); // 255
(13).toString(2).padStart(8, "0"); // "00001101"Bits, bytes and sizes
- bit — one 0 or 1.
- byte — 8 bits, 256 possible values (0–255). The smallest addressable unit of memory.
- word — the CPU's natural size, usually 64 bits today.
| n bits | Distinct values | Unsigned range |
|---|---|---|
| 8 | 256 | 0 – 255 |
| 16 | 65,536 | 0 – 65,535 |
| 32 | ~4.29 billion | 0 – 4,294,967,295 |
| 64 | ~1.8 × 10¹⁹ | 0 – 18,446,744,073,709,551,615 |
Negative numbers: two's complement
Almost every CPU stores signed integers in two's complement. With n bits, the top bit has a negative weight: −2ⁿ⁻¹.
To negate a number: invert all the bits and add 1.
- 5 =
00000101 - Invert:
11111010 - Add 1:
11111011= −5
Why this design won:
- There's only one zero (unlike sign-magnitude, which has +0 and −0).
- Addition works unchanged for signed and unsigned values: the same adder circuit handles both.
- The range of n bits is −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1. For 8 bits that's −128 to 127; for 32 bits, about ±2.1 billion.
Overflow
Fixed-size integers wrap around when they exceed their range.
int x = Integer.MAX_VALUE; // 2147483647
x + 1; // -2147483648 (silent wrap)
Math.addExact(x, 1); // throws ArithmeticException
long safe = (long) x + 1; // 2147483648# Python ints are arbitrary precision: no overflow.
2**31 + 1 # 2147483649
2**100 # works fine
# NumPy fixed-size ints DO wrap:
import numpy as np
np.int32(2**31 - 1) + np.int32(1) # -2147483648 (with a warning)// JS numbers are 64-bit floats: integers are exact only up to 2^53 - 1.
Number.MAX_SAFE_INTEGER; // 9007199254740991
2 ** 53 + 1 === 2 ** 53; // true (precision lost!)
(2 ** 31 - 1) | 0; // bitwise ops use 32-bit ints
((2 ** 31 - 1) + 1) | 0; // -2147483648
10n ** 30n; // BigInt for exact big integers- Binary is base 2; each position is a power of two.
- Hex digit = 4 bits; two hex digits = one byte.
- Convert decimal → binary by repeated division by 2.
- Two's complement: invert and add 1 to negate; the top bit is −2ⁿ⁻¹.
- Fixed-width integers overflow and wrap; know your language's behaviour.
QHow do you check whether a number is a power of two?easy
n > 0 && (n & (n - 1)) == 0. A power of two has exactly one set bit, and subtracting 1 flips that bit and all the bits below it.
QWhy is the range of a signed byte −128 to 127 rather than symmetric?medium
There are 256 patterns. Zero takes one of the non-negative patterns, leaving 127 positive values and 128 negative ones. In two's complement −128 (10000000) has no positive counterpart, so negating it overflows back to −128.