Topic 2 of 13
Number Systems & Binary Codes
Positional number systems, base conversion, signed numbers, BCD and Gray code.
← → move between slides · F full screen · M slide list
Before we start
Why this matters
Why is your phone's “gigabyte” actually 1,073,741,824 bytes — did the marketing team just round badly?
Worse than that: computers can't count to ten. They only have two fingers, off and on, so every “round number” they use looks like a phone number to the rest of us. By the end of this chapter you'll count in binary, octal and hex fluently enough to mildly annoy your friends at parties.
Outline
Topics in this chapter
Select a topic to jump to the slide that explains it.
Concept 1 of 4
Radix and positional representation
| System | Base | Symbols |
|---|---|---|
| Decimal | 10 | 0–9 |
| Binary | 2 | 0, 1 |
| Octal | 8 | 0–7 |
| Hexadecimal | 16 | 0–9, A–F |
A digit’s value depends on its position and the base:
101101₂ = 1×2⁵ + 0×2⁴ + 1×2³ + 1×2² + 0×2¹ + 1×2⁰ = 45₁₀
Concept 2 of 4
Converting between bases
- Binary to decimal: add the weights of the 1 bits.
- Decimal to binary: divide by 2 repeatedly and read the remainders from last to first.
- Binary to octal: group bits in threes from the point. Binary to hex: group in fours.
- Decimal to octal or hex directly: divide by 8 or 16 instead of 2 (fractions: multiply by 8 or 16).
- Fractions: after the point the weights are 2−1, 2−2, … To convert a decimal fraction, multiply by 2 repeatedly and read the integer parts from first to last. 101.101₂ = 5.625₁₀.
Concept 3 of 4
Binary arithmetic and signed numbers
Binary addition follows one rule at each bit position:
1-bit binary addition
| A | B | A + B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 10 (sum 0, carry 1) |
Concept 3 of 4
Binary arithmetic and signed numbers (cont.)
For multi-bit numbers, add each column right to left and propagate any carry into the column to its left — exactly like decimal addition:
| 1 | 1 | ← carries | |||||
| 1 | 0 | 1 | 0 | 1 | (21) | ||
| + | 1 | 1 | 0 | 0 | 1 | (25) | |
| 1 | 0 | 1 | 1 | 1 | 0 | (46) |
Concept 3 of 4
Binary arithmetic and signed numbers (cont.)
Subtraction borrows when 0−1 occurs.
An n-bit unsigned number ranges from 0 to 2n−1. Signed formats:
- Sign-magnitude: the MSB is the sign (0 positive, 1 negative), the rest is the magnitude.
- 1’s complement: invert every bit to negate.
- 2’s complement: invert every bit, then add 1. This is what hardware uses, because subtraction becomes addition.
8 bits: −128 … +127
Concept 4 of 4
Binary codes: BCD, Gray and ASCII
BCD writes each decimal digit in 4 bits (valid groups 0000 to 1001). 59 = 0101 1001. When you add two BCD digits, add 0110 to the group if it exceeds 1001 or produced a carry.
Gray code changes only one bit between neighbouring values. Binary to Gray: keep the first bit, then XOR each bit with the bit to its left. Gray to binary: keep the first bit, then XOR the previous binary bit with the current Gray bit. It is used in rotary encoders and in K-map ordering.
Why it matters: bit changes per step, 0–7
| Value | Binary | Bits changed | Gray | Bits changed |
|---|---|---|---|---|
| 0 | 000 | — | 000 | — |
| 1 | 001 | 1 | 001 | 1 |
| 2 | 010 | 2 | 011 | 1 |
| 3 | 011 | 1 | 010 | 1 |
| 4 | 100 | 3 | 110 | 1 |
| 5 | 101 | 1 | 111 | 1 |
| 6 | 110 | 2 | 101 | 1 |
| 7 | 111 | 1 | 100 | 1 |
Concept 4 of 4
Binary codes: BCD, Gray and ASCII (cont.)
Counting in ordinary binary sometimes flips several bits at once (e.g. 3→4 flips 3 bits); Gray code always flips exactly one bit per step, which is why it avoids read glitches in rotary encoders and gives adjacent K-map cells adjacent codes.
ASCII is a 7-bit character code: A = 1000001 (65). Codes are grouped as weighted (BCD), non-weighted (Gray, excess-3), alphanumeric (ASCII) and error-detecting (parity).
Truth tables
Truth tables (1 of 2)
Counting 0 to 15 in several systems
| Decimal | Binary | Octal | Hex | BCD | Gray |
|---|---|---|---|---|---|
| 0 | 0000 | 0 | 0 | 0000 0000 | 0000 |
| 1 | 0001 | 1 | 1 | 0000 0001 | 0001 |
| 2 | 0010 | 2 | 2 | 0000 0010 | 0011 |
| 3 | 0011 | 3 | 3 | 0000 0011 | 0010 |
| 4 | 0100 | 4 | 4 | 0000 0100 | 0110 |
| 5 | 0101 | 5 | 5 | 0000 0101 | 0111 |
| 6 | 0110 | 6 | 6 | 0000 0110 | 0101 |
| 7 | 0111 | 7 | 7 | 0000 0111 | 0100 |
| 8 | 1000 | 10 | 8 | 0000 1000 | 1100 |
| 9 | 1001 | 11 | 9 | 0000 1001 | 1101 |
| 10 | 1010 | 12 | A | 0001 0000 | 1111 |
| 11 | 1011 | 13 | B | 0001 0001 | 1110 |
| 12 | 1100 | 14 | C | 0001 0010 | 1010 |
| 13 | 1101 | 15 | D | 0001 0011 | 1011 |
| 14 | 1110 | 16 | E | 0001 0100 | 1001 |
| 15 | 1111 | 17 | F | 0001 0101 | 1000 |
Truth tables
Truth tables (2 of 2)
Three signed formats, 4 bits
| Value | Sign-magnitude | 1's complement | 2's complement |
|---|---|---|---|
| 7 | 0111 | 0111 | 0111 |
| 3 | 0011 | 0011 | 0011 |
| 1 | 0001 | 0001 | 0001 |
| 0 | 0000 | 0000 | 0000 |
| -1 | 1001 | 1110 | 1111 |
| -3 | 1011 | 1100 | 1101 |
| -7 | 1111 | 1000 | 1001 |
Interactive · one base at a time
Demo: decimal → binary, octal, hex
Interactive · one base at a time
Demo: binary → decimal, octal, hex
Interactive · one base at a time
Demo: octal → binary, decimal, hex
Interactive · one base at a time
Demo: hex → binary, decimal, octal
Interactive · all bases at once
Demo: base-conversion calculator
Interactive · custom bases
Demo: any base to any base
With no shortcut between two bases, go through decimal: (1) multiply each digit by its place value and add; (2) divide the integer part by the new base and read the remainders bottom-up, and multiply the fraction by the new base and read the integer parts top-down.
Interactive · custom bases
Demo: any base to any base (cont.)
Other ways to convert, and when to use them
- Shared power base (no decimal needed): when both bases are powers of the same number, e.g. 2, 4, 8 and 16 (powers of 2) or 3 and 9 (powers of 3), expand each digit into digits of that smaller base, then regroup. That is exactly how binary, octal and hex convert.
- Horner’s rule for step 1: work from the left, multiplying by the base and adding the next digit, e.g. 20123 = ((2·3 + 0)·3 + 1)·3 + 2 = 59. There are fewer multiplications than computing every power.
- Divide in the source base: you can repeatedly divide by the target base using the source base’s own arithmetic, but that arithmetic is unfamiliar and error-prone, so going through decimal is usually easier.
Interactive
Demo: BCD adder
Worked example 1 of 6
Worked example 1
Show the solution
- 45 ÷ 2 = 22 r 1
- 22 ÷ 2 = 11 r 0
- 11 ÷ 2 = 5 r 1
- 5 ÷ 2 = 2 r 1
- 2 ÷ 2 = 1 r 0
- 1 ÷ 2 = 0 r 1
Worked example 2 of 6
Worked example 2
Show the solution
- Column 0 (rightmost): 1+1 = 10 → write 0, carry 1.
- Column 1: 0+0+carry 1 = 1 → write 1, carry 0.
- Column 2: 1+0+carry 0 = 1 → write 1, carry 0.
- Column 3: 0+1+carry 0 = 1 → write 1, carry 0.
- Column 4 (leftmost): 1+1+carry 0 = 10 → write 0, carry 1 out.
Worked example 3 of 6
Worked example 3
Show the solution
- +5 = 00000101
- Invert: 11111010
- Add 1: 11111011
Worked example 4 of 6
Worked example 4
Show the solution
- 1000 + 0101 = 1101, which is greater than 1001.
- Add 0110: 1101 + 0110 = 1 0011.
- Result 0001 0011 = 13.
Worked example 5 of 6
Worked example 5
Show the solution
- Units: 0111 + 0101 = 1100 (12), greater than 1001 → add 0110: 1100 + 0110 = 1 0010. Digit 0010, carry 1 into the tens.
- Tens: 0001 + 1001 + carry 1 = 1011 (11), greater than 1001 → add 0110: 1011 + 0110 = 1 0001. Digit 0001, carry 1 into the hundreds.
- Hundreds: 0100 + 0001 + carry 1 = 0110 (6), not greater than 1001 → no correction needed. Digit 0110, no carry out.
Worked example 6 of 6
Worked example 6
Show the solution
- First bit: 1
- 1 ⊕ 0 = 1
- 0 ⊕ 1 = 1
- 1 ⊕ 1 = 0
Practice
Practice (1 of 2)
Try each one on paper first, then check your answer.
Show the answer
Show the answer
Show the answer
Practice
Practice (2 of 2)
Try each one on paper first, then check your answer.
Show the answer
Show the answer
Show the answer
Use the Number input and Number output blocks to watch binary values, and the bit splitter and combiner to split a number into bits.
Gate ForgeA free circuit simulator — place and wire gates, generate truth tables and timing diagrams, and work through 90+ guided problems.Check yourself
Quiz
Summary
Summary
- Value = Σ digit × baseposition.
- One octal digit = 3 bits, one hex digit = 4 bits.
- 2’s complement: invert and add 1. n-bit range −2n−1 to 2n−1−1.
- BCD addition: add 0110 when a group exceeds 1001 or carries out.
- Gray code changes one bit per step: G = B ⊕ (B shifted right).