Binary numbers and data representation

AP Computer Science Principles · lesson 2 of 4 · 20 min

Why binary?

Computers store and process everything — numbers, text, images, sound — as sequences of bits (binary digits). A bit is the smallest unit of data and can hold exactly one of two values: 0 or 1. This maps directly to the physical reality of transistors, which are either off (0) or on (1). Eight bits form a byte . A byte can represent 2⁸ = 256 distinct values (0 through 255). A kilobyte is 1,024 bytes, a megabyte is 1,024 KB, and so on.

Place value in binary

Binary is base-2. Each position represents a power of 2, increasing from right to left: 2⁰, 2¹, 2², 2³, … To convert binary 10110101 to denary: identify which bits are 1, then add their place values. 128 + 32 + 16 + 4 + 1 = 181

Converting denary to binary

Repeatedly divide by 2 and record the remainders. Read the remainders from bottom to top. Worked example: convert 45 to binary

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 remainders bottom to top: 101101

Verify: 32 + 8 + 4 + 1 = 45 ✓

Hexadecimal

Hexadecimal (base-16) is a compact way to represent binary. Each hex digit represents exactly 4 bits (a nibble). Digits 0–9 represent values 0–9; A–F represent 10–15. To convert binary to hex, group bits into nibbles from the right, then replace each nibble with its hex digit. 10110101 → 1011 0101 → B5.

Data compression

Lossless compression reduces file size without losing any data. The original file can be perfectly reconstructed. ZIP and PNG use lossless compression. Lossy compression permanently discards some data to achieve much smaller file sizes. JPEG and MP3 use lossy compression.

Knowledge check

  • Question of
  • Hide hint
  • Show hint

✓ Correct! ✕ Not quite

  • Try again
  • See results
  • Next question
  • Check answer

Previous lesson: Computational thinking and problem decomposition
Back to AP Computer Science Principles
Next lesson: Conditionals and Boolean logic