Home
2Number Systems & Binary Codes
2

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.

Concept 1 of 4

Radix and positional representation

SystemBaseSymbols
Decimal100–9
Binary20, 1
Octal80–7
Hexadecimal160–9, A–F

A digit’s value depends on its position and the base:

345₁₀ = 3×10² + 4×10¹ + 5×10⁰
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₁₀.
DecimalBinaryOctalHexadecimal÷2 / ×2group×3group×4÷8 / ×8÷16 / ×16
Solid: through binary. Dotted: straight from decimal, dividing (or, for fractions, multiplying) by 8 or 16.

Concept 3 of 4

Binary arithmetic and signed numbers

Binary addition follows one rule at each bit position:

1-bit binary addition

ABA + B
000
011
101
1110 (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:

11← carries
10101(21)
+11001(25)
101110(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.
n-bit 2’s complement range: −2ⁿ⁻¹ … 2ⁿ⁻¹ − 1
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

ValueBinaryBits changedGrayBits changed
0000—000—
100110011
201020111
301110101
410031101
510111111
611021011
711111001

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.

BinaryGray0000000010010100110110101001101011111101011111001 bit changes1 bit changes
The 3-bit Gray sequence is a “reflected” binary count: reading down, each Gray-code row differs from the one above it in exactly one bit position, unlike ordinary binary where several bits can flip at once (highlighted for two adjacent pairs).

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

DecimalBinaryOctalHexBCDGray
00000000000 00000000
10001110000 00010001
20010220000 00100011
30011330000 00110010
40100440000 01000110
50101550000 01010111
60110660000 01100101
70111770000 01110100
810001080000 10001100
910011190000 10011101
10101012A0001 00001111
11101113B0001 00011110
12110014C0001 00101010
13110115D0001 00111011
14111016E0001 01001001
15111117F0001 01011000

Truth tables

Truth tables (2 of 2)

Three signed formats, 4 bits

ValueSign-magnitude1's complement2's complement
7011101110111
3001100110011
1000100010001
0000000000000
-1100111101111
-3101111001101
-7111110001001

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

Convert 45₁₀ to binary.
Show the solution
  1. 45 ÷ 2 = 22 r 1
  2. 22 ÷ 2 = 11 r 0
  3. 11 ÷ 2 = 5 r 1
  4. 5 ÷ 2 = 2 r 1
  5. 2 ÷ 2 = 1 r 0
  6. 1 ÷ 2 = 0 r 1
Read the remainders bottom to top: 101101₂.

Worked example 2 of 6

Worked example 2

Add 10101₂ + 11001₂, showing the carry into each column.
Show the solution
  1. Column 0 (rightmost): 1+1 = 10 → write 0, carry 1.
  2. Column 1: 0+0+carry 1 = 1 → write 1, carry 0.
  3. Column 2: 1+0+carry 0 = 1 → write 1, carry 0.
  4. Column 3: 0+1+carry 0 = 1 → write 1, carry 0.
  5. Column 4 (leftmost): 1+1+carry 0 = 10 → write 0, carry 1 out.
Result 101110₂ = 46 (21 + 25 = 46, checked in decimal).

Worked example 3 of 6

Worked example 3

Write −5 in 8-bit 2’s complement.
Show the solution
  1. +5 = 00000101
  2. Invert: 11111010
  3. Add 1: 11111011

Worked example 4 of 6

Worked example 4

Add 8 + 5 in BCD.
Show the solution
  1. 1000 + 0101 = 1101, which is greater than 1001.
  2. Add 0110: 1101 + 0110 = 1 0011.
  3. Result 0001 0011 = 13.

Worked example 5 of 6

Worked example 5

Add 417 + 195 in BCD (multi-digit, with a carry between digits).
Show the solution
  1. Units: 0111 + 0101 = 1100 (12), greater than 1001 → add 0110: 1100 + 0110 = 1 0010. Digit 0010, carry 1 into the tens.
  2. Tens: 0001 + 1001 + carry 1 = 1011 (11), greater than 1001 → add 0110: 1011 + 0110 = 1 0001. Digit 0001, carry 1 into the hundreds.
  3. Hundreds: 0100 + 0001 + carry 1 = 0110 (6), not greater than 1001 → no correction needed. Digit 0110, no carry out.
Result: 0110 0001 0010 = 612. Each digit is corrected independently, and a carry out of one digit feeds straight into the next digit's addition — exactly like carrying in ordinary decimal addition, just wrapped in 4-bit groups.

Worked example 6 of 6

Worked example 6

Convert binary 1011 to Gray code.
Show the solution
  1. First bit: 1
  2. 1 ⊕ 0 = 1
  3. 0 ⊕ 1 = 1
  4. 1 ⊕ 1 = 0
Gray code: 1110.

Practice

Practice (1 of 2)

Try each one on paper first, then check your answer.

1. Convert 101101₂ to decimal.
Show the answer
45
2. Find the 2’s complement of 00101101.
Show the answer
11010011 (invert to 11010010, add 1).
3. Write 59 in BCD.
Show the answer
0101 1001

Practice

Practice (2 of 2)

Try each one on paper first, then check your answer.

4. Perform the BCD addition 8 + 5.
Show the answer
0001 0011 (13)
5. Convert 1011 to Gray code.
Show the answer
1110
6. Convert 11010110₂ to octal and hexadecimal.
Show the answer
Octal: 011 010 110 = 326₈. Hex: 1101 0110 = D6₁₆.
Practice in Gate Forge

Use the Number input and Number output blocks to watch binary values, and the bit splitter and combiner to split a number into bits.

Open Gate ForgeGate 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).