How Binary and Hexadecimal Work (Conversion and Arithmetic)
Binary is base 2 (digits 0 and 1) and hexadecimal is base 16 (digits 0–9 and A–F); each hex digit equals exactly four binary digits. 1101₂ = 8 + 4 + 1 = 13, FF₁₆ = 255, and 2F₁₆ = 0010 1111₂ = 47. Adding 1010 + 0110 in binary gives 10000, which is 16.
Place values in any base
In decimal, each place is worth ten times the one to its right: 1, 10, 100, 1,000. A base-b system uses b digits and each place is worth b times the previous one. Binary places are 1, 2, 4, 8, 16, 32, 64, 128; hexadecimal places are 1, 16, 256, 4,096.
Binary to decimal
Add the place values where there is a 1. For 1101: 8 + 4 + 0 + 1 = 13. For 11111111: 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255, the largest value of one byte.
Decimal to binary
Divide by 2 repeatedly and note the remainders, then read them from the last to the first. For 13: 13 ÷ 2 = 6 r 1; 6 ÷ 2 = 3 r 0; 3 ÷ 2 = 1 r 1; 1 ÷ 2 = 0 r 1. Reading upward gives 1101.
Hexadecimal
Base 16 needs sixteen symbols, so the letters A to F stand for 10 to 15. Place values are powers of 16, so 2F = 2 × 16 + 15 = 47 and 1A3 = 256 + 160 + 3 = 419. To convert decimal to hex, divide by 16: 419 ÷ 16 = 26 r 3; 26 ÷ 16 = 1 r 10 (A); 1 ÷ 16 = 0 r 1; reading upward gives 1A3.
Binary and hex are linked
Because 16 = 2⁴, one hex digit always equals four bits. To convert 2F to binary, write 2 = 0010 and F = 1111 to get 0010 1111. To go back, group the bits in fours from the right and convert each group. This is why programmers use hex as shorthand for binary.
| Decimal | Binary | Hex |
|---|---|---|
| 0 | 0000 | 0 |
| 5 | 0101 | 5 |
| 10 | 1010 | A |
| 15 | 1111 | F |
| 255 | 1111 1111 | FF |
Binary arithmetic
Binary addition follows 0 + 0 = 0, 0 + 1 = 1 and 1 + 1 = 10 (write 0, carry 1). For 1010 + 0110: the result is 10000. Multiplying by 2 is a shift left (1 → 10 → 100), and dividing by 2 is a shift right.
Bitwise operations
AND keeps a 1 only when both bits are 1 (1100 AND 1010 = 1000). OR gives 1 when either bit is 1 (1110). XOR gives 1 when the bits differ (0110). These are used for flags, permissions and masks: AND with 0F keeps only the low four bits of a byte.
Where you meet them
- Memory sizes and storage: a byte is 8 bits, and 2⁶⁴ = 18446744073709551616.
- Colours: #FF8800 means red 255, green 136, blue 0.
- Network addresses: IPv4 addresses are four bytes, and IPv6 and MAC addresses are written in hex.
- Character codes: the letter A is 65 = 41 in hex = 01000001 in binary.
Practise with the binary calculator and the hex calculator, and see the IP subnet calculator for binary in networking.
Key terms
- Bit: One binary digit.
- Byte: Eight bits.
- Hexadecimal: Base 16, with digits 0 to 9 and A to F.
- Bitwise AND: Gives 1 only where both bits are 1.
- Nibble: Four bits, one hex digit.
Practice questions
Try each one before you read the answer.
- Convert 1011 from binary to decimal.
Answer: 8 + 2 + 1 = 11. - What is 2A in decimal?
Answer: 2 × 16 + 10 = 42. - Which hex digit is 1110 in binary?
Answer: E.
Frequently asked questions
Why do computers use binary?
Electronic components have two reliable states, on and off, which map directly to 1 and 0, making binary simple and dependable in hardware.
What is the largest number in 8 bits?
255, or 11111111 in binary. With 8 bits you can store 256 different values from 0 to 255.
Why is hexadecimal popular with programmers?
It is a compact way to write binary: every four bits become one hex digit, so a byte is two hex digits.