DP Computer Science · Representation

Binary & hexadecimal

See the place. Follow the steps. Explain the code.

One value, three representations

42₁₀ = 101010₂ = 2A₁₆. The subscripts name the base; they are not part of the number. This lesson covers unsigned whole numbers. Negative numbers, two’s complement and fractions need additional rules.

1 · Place value

In base b, the rightmost place is b⁰ = 1. Moving one place left multiplies the weight by b. A digit contributes digit × weight; add all contributions.

Decimal · base 10

42 = 4 × 10¹ + 2 × 10⁰

Binary · base 2

101010₂ = 32 + 8 + 2 = 42₁₀

Hexadecimal · base 16

2A₁₆ = 2 × 16¹ + 10 × 16⁰ = 42₁₀

Binary digits are 0–1. Decimal digits are 0–9. Hexadecimal digits are 0–9 and A–F, where A=10, B=11, C=12, D=13, E=14 and F=15. The largest digit is always base − 1.

2 · Conversion studio

Choose a method and enter a value. Predict each next step before revealing it. Play advances once every 1.2 seconds; Pause, Previous, Next and Reset give you control. Nothing plays automatically.

3 · Worked conversions

Binary → decimal

101010₂: 1×32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1 = 42₁₀

Write weights from the right: 1, 2, 4, 8, 16, 32. Keep zero columns so the other digits stay aligned.

Decimal → binary

42 ÷ 2 = 21 r0
21 ÷ 2 = 10 r1
10 ÷ 2 = 5 r0
5 ÷ 2 = 2 r1
2 ÷ 2 = 1 r0
1 ÷ 2 = 0 r1
42₁₀ = 101010₂

Divide the quotient again until it is zero. Read remainders from bottom to top: the first remainder is the rightmost digit. For zero, write 0.

Hexadecimal ↔ decimal

FF₁₆ = 15×16 + 15 = 255₁₀
255 ÷ 16 = 15 r15 (F)
15 ÷ 16 = 0 r15 (F)
255₁₀ = FF₁₆

Use powers of 16 to decode hexadecimal. To encode decimal, divide repeatedly by 16 and reverse the remainders; replace 10–15 with A–F.

Binary ↔ hexadecimal

101010₂ → 0010 1010 → 2 A → 2A₁₆
2A₁₆ → 0010 1010₂ → 101010₂

Because 16 = 2⁴, one hex digit matches exactly four bits. Group binary from the right; pad only the leftmost group with zeros. Expand every hex digit to four bits, including inner zero groups.

4 · Python: predict, run, explain

These are worked Python 3 examples with exact output. Copy them into the existing Python playground, predict the result, then run them.

Open Python playground →

Built-in conversions

n = 42
print(bin(n), hex(n))
print(format(n, "08b"), format(n, "02X"))
print(int("101010", 2), int("2A", 16))

Output

0b101010 0x2a
00101010 2A
42 42

bin() and hex() return strings with prefixes. format() can omit the prefix, use uppercase hex, and pad to a minimum width. int(text, base) parses a string into an integer. Padding does not change the value or impose an 8-bit limit.

Build the algorithm

def to_base(n, base):
    if n < 0 or base not in (2, 16):
        raise ValueError("Use non-negative integers and base 2 or 16")
    if n == 0:
        return "0"
    digits = "0123456789ABCDEF"
    result = ""
    while n > 0:
        n, remainder = divmod(n, base)
        result = digits[remainder] + result
    return result

for n in [0, 10, 15, 16, 42, 255]:
    print(n, to_base(n, 2), to_base(n, 16))

Output

0 0 0
10 1010 A
15 1111 F
16 10000 10
42 101010 2A
255 11111111 FF

The function expects integer arguments. divmod() gives the quotient and remainder. Prepending each digit reverses the remainder order. The zero case prevents an empty result. Trace n=16: hexadecimal carries into a new place as 10, while binary becomes 10000.

5 · Catch the mistakes

  • 10₂ is 2₁₀, but 10₁₆ is 16₁₀. Always state the base.
  • Start powers at zero, not one. The rightmost digit always has weight 1.
  • Do not read division remainders top-to-bottom. Reverse them.
  • Do not group bits from the left or pad on the right: that can change the value. 101₂ = 0101₂, but 1010₂ is twice as large.
  • A is one hexadecimal digit with value 10, not two decimal digits. G and binary digit 2 are invalid.
  • Leading zeros preserve an unsigned value; they may still matter when showing a fixed width. Eight unsigned bits hold 0–255; 256 needs nine bits.
Check yourself: convert 10000001₂ and 3F₁₆ to decimal.

10000001₂ = 128 + 1 = 129₁₀; 3F₁₆ = 3 × 16 + 15 = 63₁₀.