4.2 Binary, hexadecimal & data representation
Key Takeaways
- A bit is one 0 or 1; a nibble is 4 bits; a byte is 8 bits and holds 256 possible values (0-255).
- Convert binary to decimal by adding the place values (powers of two) of the 1 bits; convert decimal to binary by repeated division by 2.
- One hex digit equals exactly one 4-bit nibble, so binary 1011 = B and 0xFF = 255 = binary 11111111.
- ASCII maps characters to 7-bit numbers ('A' is 65, 'a' is 97); UTF-8/Unicode extends this while keeping the first 128 codes identical.
- Hexadecimal is used for MAC (48-bit) and IPv6 (128-bit) addresses because it makes long bit strings short and readable.
Why Computers Count in Twos
At the lowest level a computer stores everything as bits - binary digits that are either 0 or 1, mirroring a switch that is off or on. Because there are only two symbols, this is the binary (base-2) number system. Binary is used because digital hardware is built from switches that are reliably either off or on; representing more than two states on one wire would be far harder to keep accurate. Everything you see - numbers, text, images, and sound - is ultimately stored as long strings of these 0s and 1s. Humans normally use decimal (base-10) with ten symbols (0-9). Programmers also use hexadecimal (base-16), which has sixteen symbols: 0-9, then A, B, C, D, E, F for the values ten through fifteen.
Bits, nibbles, and bytes
- A bit is one binary digit.
- A nibble is 4 bits.
- A byte is 8 bits - the standard unit for one character of text.
An 8-bit byte can represent 2^8 = 256 different values, from 0 to 255. Knowing the powers of two is essential:
| Power | Value |
|---|---|
| 2^0 | 1 |
| 2^1 | 2 |
| 2^2 | 4 |
| 2^3 | 8 |
| 2^4 | 16 |
| 2^5 | 32 |
| 2^6 | 64 |
| 2^7 | 128 |
| 2^8 | 256 |
| 2^9 | 512 |
| 2^10 | 1024 |
Each position in a binary number is worth a power of two, doubling from right to left: 1, 2, 4, 8, 16, and so on. Counting in binary works like decimal but rolls over after 1 instead of 9: 0, 1, 10, 11, 100, 101, 110, 111 are zero through seven.
The 0-15 conversion table
Because one hex digit maps to exactly one nibble (4 bits), hex is a compact shorthand for binary. Memorize this table:
| Decimal | Binary | Hex |
|---|---|---|
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 3 |
| 4 | 0100 | 4 |
| 5 | 0101 | 5 |
| 6 | 0110 | 6 |
| 7 | 0111 | 7 |
| 8 | 1000 | 8 |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 11 | 1011 | B |
| 12 | 1100 | C |
| 13 | 1101 | D |
| 14 | 1110 | E |
| 15 | 1111 | F |
Worked example 1: binary to decimal
Convert 1011 to decimal. Write each bit's place value and add the ones that are set:
- Place values (left to right): 8, 4, 2, 1
- (1x8) + (0x4) + (1x2) + (1x1) = 8 + 2 + 1 = 11
So 1011 in binary equals 11 in decimal - and from the table, that is B in hex.
Worked example 2: decimal to binary
Convert decimal 45 to binary by repeated division by 2, reading the remainders bottom-to-top:
- 45 / 2 = 22 remainder 1
- 22 / 2 = 11 remainder 0
- 11 / 2 = 5 remainder 1
- 5 / 2 = 2 remainder 1
- 2 / 2 = 1 remainder 0
- 1 / 2 = 0 remainder 1
Reading the remainders from the last up gives 101101. Check: 32 + 8 + 4 + 1 = 45. Correct.
Worked example 3: binary to hex
Convert 101101 to hex. Group the bits into nibbles of 4 starting from the right, padding the left with zeros: 0010 1101. From the table, 0010 = 2 and 1101 = D, so 101101 = 0x2D. (The "0x" prefix marks a hex number.) Converting 0x2D back to decimal: (2x16) + 13 = 32 + 13 = 45 - matching the earlier result.
Worked example 4: hex to decimal, and a full byte
Convert 0xFF to decimal. Each hex digit is a power of 16: (F x 16) + (F x 1) = (15 x 16) + 15 = 240 + 15 = 255 - the largest value one byte can hold, or binary 11111111. As a final check, take the byte 11001010: the 1 bits sit in the 128, 64, 8, and 2 places, so 128 + 64 + 8 + 2 = 202; its two nibbles 1100 and 1010 are C and A, so 11001010 = 0xCA.
Representing Text: ASCII
Numbers are natural in binary, but text needs a code that maps characters to numbers. ASCII (American Standard Code for Information Interchange) assigns each letter, digit, and symbol a 7-bit number (0-127). For example, uppercase 'A' is 65 (binary 1000001), and letters continue in order, so 'B' is 66 and 'C' is 67. Lowercase 'a' is 97, and the digit character '0' is 48. Because uppercase and lowercase differ by exactly 32, flipping a single bit changes the case. Modern systems extend this idea with Unicode/UTF-8, which can encode every language's characters while keeping the first 128 codes identical to ASCII.
Why hex for MAC and IPv6 addresses
Hexadecimal appears throughout networking because it packs four bits into one readable digit. A MAC address is 48 bits, written as six pairs of hex digits like 00:1A:2B:3C:4D:5E. An IPv6 address is 128 bits, written as eight groups of four hex digits, such as 2001:0db8:85a3::8a2e:0370:7334. Writing those long bit strings in raw binary would be error-prone; hex keeps them short and readable while still mapping cleanly back to the underlying bits.
What is the decimal value of the binary number 1011?
How many different values can a single 8-bit byte represent?
Why are hexadecimal digits used to write MAC and IPv6 addresses?