Counting with two digits
We count in tens because we have ten fingers. There are ten digits, 0 to 9, and when we run out we add a column: after 9 comes 10.
A computer is built from switches that are either off or on. It has two "fingers". So it counts with two digits, 0 and 1, and adds a column when it runs out: after 1 comes 10.
That is binary, or base 2. Each binary digit is a bit.
Place values
In the decimal number 352, each position is worth ten times the one to its right: 3 hundreds, 5 tens, 2 ones.
Binary works the same way, with each position worth twice the one to its right: 1, 2, 4, 8, 16, 32, 64, 128.
Flip the bits and watch the number change.
32 + 8 + 2 = 42. Each bit that is on adds its place value. Click any bit to flip it. In hexadecimal, the left four bits make the digit 2 and the right four make A.
- Decimal
- 42
- Binary
- 0010 1010
- Hexadecimal
- 0x2A
Example
Reading 0010 1010
Write the place values above the bits:
128, 64, 32, 16, 8, 4, 2, 1
The bits that are on are worth 32, 8 and 2. Add them: 42.
Try a few things in the demo.
- Press + 1 several times and watch the carry ripple to the left, exactly like 99 becoming 100.
- Press × 2. Every bit moves one place left. Doubling in binary is a shift, just as multiplying by ten in decimal adds a zero.
- Switch all on. The largest number 8 bits can hold is 255.
How much fits in n bits
Each extra bit doubles the number of values.
| Bits | Different values | Typical use |
|---|---|---|
| 1 | 2 | Yes or no |
| 8 (one byte) | 256 | One colour channel, one basic character |
| 16 | 65,536 | Older character sets, audio samples |
| 32 | About 4.3 billion | IPv4 addresses, many integers |
| 64 | About 18 billion billion | Modern integers and memory addresses |
Eight bits make a byte, the basic unit of memory and file sizes.
Hexadecimal: binary for humans
Binary is exact, and tiresome to read. 11111111 00000000 10101010 is three bytes, and you already lost your place.
Hexadecimal, base 16, fixes that. It has sixteen digits: 0 to 9, then A to F for the values ten to fifteen.
| Decimal | Binary | Hex |
|---|---|---|
| 0 | 0000 | 0 |
| 5 | 0101 | 5 |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 11 | 1011 | B |
| 12 | 1100 | C |
| 13 | 1101 | D |
| 14 | 1110 | E |
| 15 | 1111 | F |
Here is why programmers love it: sixteen is 2 × 2 × 2 × 2, so one hex digit is exactly four bits. A byte is always two hex digits. Converting is a matter of looking up each group of four, with no arithmetic.
Example
0010 1010 in hex
Split into groups of four: 0010 and 1010.
0010 is 2. 1010 is A.
So the byte is 2A, usually written 0x2A to show it is hexadecimal. In the demo, the hex value follows the two halves of the row.
Those three bytes from before become FF 00 AA.
Where you will see it
- Colours.
#FF8800is three bytes: red FF (255), green 88 (136), blue 00 (0). A strong orange. - Memory addresses, such as
0x7ffd4a2c, in debuggers and error messages. - Hashes and ids. A commit id like
3f9a1c2or a UUID is a long number written in hex. - Bytes of any kind, in a file dump or a network trace.
- Permissions and flags. Each bit is a separate yes or no, packed into one number.
Working with bits
Because each bit is independent, a single number can hold many on-off settings, and they can be tested and changed with bitwise operations.
READ, WRITE, EXECUTE = 4, 2, 1 # 100, 010, 001 in binary
permissions = READ | WRITE # OR switches bits on: 110 = 6
can_write = permissions & WRITE # AND tests a bit: non-zero means yes
permissions = permissions ^ WRITE # XOR flips a bit: 100 = 4
doubled = 5 << 1 # shift left: 101 becomes 1010 = 10Clicking a bit in the demo is an XOR with that bit's place value.
Key takeaways
- Binary is counting with two digits. Each position is worth twice the one to its right.
- A number's value is the sum of the place values of the bits that are on.
- n bits can hold 2 to the power n different values. 8 bits is a byte, with 256 values.
- Hexadecimal uses sixteen digits, and one hex digit is exactly four bits.
- Hex is how bytes are written for people: colours, addresses, hashes and ids.