AP Computer Science Principles • Data

Binary Numbers

Understanding how computers represent all data using only two digits: 0 and 1.

Computer science principles illustration with a network, database, and code file
Inside this lesson 9 sections 5 questions 8 figures

SECTION 1

Historical Context & Motivation

The idea of representing quantities with only two symbols predates modern computing by centuries. Long before transistors and silicon chips, mathematicians recognized that any number expressible in our familiar base-10 (decimal) system could equivalently be written in base-2 (binary) — a system requiring only the digits 0 and 1. This insight became the cornerstone of digital computing because electronic circuits naturally distinguish between two voltage states: high and low, on and off, true and false. The elegance of binary lies in its alignment with the physical properties of hardware: a single binary digit, or bit, maps directly to the smallest unit of information a machine can store.

1679 • Leibniz's Binary Arithmetic

Gottfried Wilhelm Leibniz formally documented a complete binary numeral system, demonstrating that arithmetic operations could be performed with just 0 and 1.

1854 • Boole's Algebra of Logic

George Boole published An Investigation of the Laws of Thought, formalizing logical operations (AND, OR, NOT) over two-valued truth systems—the theoretical basis for digital logic gates.

1937 • Shannon's Master's Thesis

Claude Shannon showed that Boolean algebra could be implemented with electrical relay circuits, bridging abstract binary math and physical hardware design.

1945 • Von Neumann Architecture

John von Neumann's stored-program architecture adopted binary encoding for both instructions and data, establishing the paradigm still used in virtually all modern computers.

The central question this lesson addresses is deceptively simple: how does a machine that recognizes only two electrical states represent the vast universe of numbers, text, images, and sound? The answer begins with understanding positional notation in base-2 and recognizing that every piece of digital data — from a single character to a streaming video — ultimately reduces to sequences of binary digits.

SECTION 2

Core Principles & Definitions

Binary is a positional number system — the value of each digit depends on its position within the number, just as in decimal. In decimal, positions correspond to powers of 10; in binary, positions correspond to powers of 2. Understanding this single structural parallel unlocks nearly every conversion and arithmetic operation you will need for the AP exam.

1

Bit

A bit (binary digit) is the smallest unit of data, holding a value of 0 or 1. Every other data representation is built from sequences of bits.
2

Byte

A byte is a group of 8 bits, capable of representing 2⁸ = 256 distinct values (0–255). Bytes are the standard unit for measuring data storage.
3

Place Value (Powers of 2)

Each position in a binary number represents a successive power of 2, starting from 2⁰ = 1 at the rightmost bit and increasing leftward: 2¹, 2², 2³, and so on.
4

Overflow

Overflow occurs when a calculation produces a result that requires more bits than are available. With n bits, the maximum unsigned value is 2ⁿ − 1.

SECTION 3

Visual Explanation — Place Value Diagram

8-Bit Binary Place ValuesBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 02⁷2⁶2⁵2⁴2³2²2¹2⁰1286432168421Example: 10110101₂ = ?10110101128—3216—4—1128 + 32 + 16 + 4 + 1 = 181Green digits are ON (1) · Red digits are OFF (0)
The diagram shows an 8-bit binary number with each position labeled by its power of 2 and its decimal equivalent. Green digits contribute their place value to the total; red digits contribute nothing. Summing the active place values yields the decimal result.

The place-value diagram above makes the conversion algorithm explicit. Starting from the leftmost bit (the most significant bit) and moving right to the least significant bit, you multiply each digit by the corresponding power of 2 and sum the products. Any position holding a 0 contributes zero to the total, so only the 1-bits matter. This direct mapping between bit position and power of 2 is the conceptual key to every binary-to-decimal conversion.

SECTION 4

Mathematical Framework

BINARY TO DECIMAL CONVERSION

V=bn−1×2n−1+bn−2×2n−2+…+b1×21+b0×20V = bₙ₋₁ × 2ⁿ⁻¹ + bₙ₋₂ × 2ⁿ⁻² + … + b₁ × 2¹ + b₀ × 2⁰

where V is the decimal value, n is the number of bits, and bᵢ ∈ {0, 1} is the digit at position i.

RANGE OF AN N-BIT UNSIGNED INTEGER

0≤V≤2n−10 ≤ V ≤ 2ⁿ − 1

An n-bit binary number can represent 2ⁿ distinct values. For example, 8 bits represent 0 through 255, and 16 bits represent 0 through 65,535.

DECIMAL TO BINARY (REPEATED DIVISION)

Divideby2repeatedly;recordremaindersfrombottomtotop.Divide by 2 repeatedly; record remainders from bottom to top.

At each step, divide the quotient by 2. The remainder (0 or 1) becomes the next bit, starting at the least significant position. Continue until the quotient reaches 0.

Binary arithmetic follows the same rules as decimal arithmetic, with a simpler addition table: 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, and 1 + 1 = 10 (i.e., 0 with a carry of 1). When carries cascade—as in 1111 + 0001—every bit position generates a carry, and the result overflows if there are not enough bits to hold the final carry. Recognizing overflow conditions is an important exam skill: adding two n-bit numbers can produce a result requiring n + 1 bits.

SECTION 5

Detailed Breakdown — Conversion Methods

Decimal 53 → Binary (Repeated Division)DivisionQuotientRemainderBit53 ÷ 2261b₀26 ÷ 2130b₁13 ÷ 261b₂6 ÷ 230b₃3 ÷ 211b₄1 ÷ 201b₅Read remainders bottom → top:110101₂Verification: 32 + 16 + 4 + 1 = 53 ✓read↑ up
The repeated-division method converts decimal 53 to binary by dividing by 2 at each step and recording the remainder. Reading remainders from bottom to top yields 110101₂.

The repeated-division algorithm is systematic and mechanical, but for numbers that are close to powers of 2 or that you can decompose by inspection, a subtraction method is often faster. Start with the largest power of 2 that does not exceed the target, place a 1 in that position, subtract, and repeat with the remainder. For example, 200 = 128 + 64 + 8, so 200₁₀ = 11001000₂. Both approaches yield the same result; familiarity with both lets you choose the quicker path on a timed exam.

DecimalBinaryBit Count
001
71113
1511114
16100005
255111111118
2561000000009

Common decimal-to-binary reference values

SECTION 6

Worked Example — Binary Addition & Overflow

Worked example

  1. 1

    Step 1 — Interpret the operands

    Convert each binary number to decimal to verify your result later. 10110011₂ = 128 + 32 + 16 + 2 + 1 = 179. 01101010₂ = 64 + 32 + 8 + 2 = 106.
    179 + 106 = 285 (expected decimal sum)
  2. 2

    Step 2 — Align and add column by column

    Starting from the rightmost bit (b₀), add corresponding digits plus any carry from the previous column. In the b₀ column: 1 + 0 = 1, carry = 0. In b₁: 1 + 1 = 0, carry = 1. Continue across all eight columns, propagating carries as needed.
  3. 3

    Step 3 — Record the result

    The column-by-column addition produces: carry out = 1, and the 8-bit result = 00011101₂. The carry out of 1 indicates that the true sum requires 9 bits: 100011101₂ = 285.
    Full 9-bit result: 100011101₂ = 285₁₀
  4. 4

    Step 4 — Detect overflow

    Because the system is limited to 8 bits, the carry bit is lost. The stored value would be 00011101₂ = 29, not 285. This is a classic overflow error — the result exceeds the maximum 8-bit unsigned value of 255.
    Overflow detected: 8-bit result wraps to 29

SECTION 7

Number Bases Compared — Binary, Decimal & Hexadecimal

PropertyBinary (Base 2)Decimal (Base 10)Hexadecimal (Base 16)
Digits used0, 10–90–9, A–F
Place value factor× 2× 10× 16
Representation of 25511111111255FF
Primary useHardware / logic gatesHuman communicationCompact binary shorthand (colors, memory addresses)
StrengthsMaps directly to electrical states; simplest arithmetic rulesIntuitive for humans; familiarEach hex digit = 4 bits; very compact
LimitationsLong strings for large values; hard for humans to readNo direct hardware analogLess intuitive than decimal; extra letter-digits needed

SECTION 8

Connection to Advanced Representations

Unsigned binary numbers, which represent only non-negative integers, are the foundation upon which more sophisticated data representations are built. The AP CSP curriculum expects you to recognize that binary encoding extends far beyond simple counting. Numbers with fractional parts, negative numbers, characters, colors, and audio samples all rely on agreed-upon abstractions layered on top of binary sequences.

Data TypeBinary MechanismKey Idea
Negative integersTwo's complement: flip bits and add 1The MSB becomes a sign bit; range shifts to −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1
Real numbersFloating-point (IEEE 754): sign + exponent + mantissaFinite bits approximate infinite precision; rounding errors are inherent
Text charactersASCII (7 bits) / Unicode (up to 32 bits)Each character is assigned a unique numeric code expressed in binary
ColorsRGB: 8 bits per channel (24 bits total)Over 16 million colors from combinations of red, green, and blue

The overarching principle is that binary alone does not inherently mean anything — meaning arises from the abstraction layer that interprets the bits. The same 8-bit sequence 01000001 could represent the unsigned integer 65, the ASCII character 'A', or part of a pixel's color, depending on context. This idea — that data requires agreed-upon encoding schemes to be meaningful — is a recurring theme throughout AP Computer Science Principles.

SECTION 9

Practice Problems

Question 1: CONCEPTUAL

Which of the following best explains why computers use binary (base-2) to represent data?
  • A. Binary requires fewer digits than decimal to represent the same number.
  • B. Electronic circuits can reliably distinguish between two voltage states (high and low), making binary a natural fit for hardware.
  • C. Binary is the only number system that supports arithmetic operations like addition and subtraction.
  • D. Humans find binary easier to read and compute with than decimal.
Show answer (B)
B is correct. Computers use binary because transistors operate as two-state switches (on/off), and reliably distinguishing two voltage levels is far simpler and more fault-tolerant than distinguishing ten. A is wrong because binary actually requires more digits (bits) than decimal for the same value (e.g., 255₁₀ needs 8 bits vs. 3 decimal digits). C is wrong because arithmetic works in any base — decimal, octal, hexadecimal, etc. D is wrong because humans overwhelmingly find decimal more intuitive; binary's advantage is for machines, not people.

Question 2: BASIC CALCULATION

What is the decimal value of the binary number 11010110₂?
  • A. 198
  • B. 214
  • C. 218
  • D. 204
Show answer (B)
B is correct. Expand by place value: 1×128 + 1×64 + 0×32 + 1×16 + 0×8 + 1×4 + 1×2 + 0×1 = 128 + 64 + 16 + 4 + 2 = 214. A (198) results from incorrectly omitting the 16 place value. C (218) results from incorrectly including the 4 twice or miscounting. D (204) results from skipping the 2¹ and 2⁴ positions. Careful place-value accounting avoids these errors.

Question 3: INTERMEDIATE

A computer uses 4-bit unsigned binary to store values. Which TWO of the following statements are true about this system?
  • A. The largest value that can be represented is 16.
  • B. Adding 1 to 1111₂ produces an overflow.
  • C. There are exactly 16 distinct values that can be represented.
  • D. The binary representation of 9 requires 5 bits and cannot be stored.
Show answer (B,C)
B and C are correct. With 4 bits, 2⁴ = 16 distinct values exist (0 through 15), confirming C. Adding 1 to 1111₂ (15) would yield 10000₂ (16), which requires 5 bits — the carry is lost, producing overflow, confirming B. A is wrong because the largest 4-bit unsigned value is 2⁴ − 1 = 15, not 16. D is wrong because 9₁₀ = 1001₂, which requires only 4 bits and fits within the system.

Question 4: APPLIED

A digital camera sensor records each pixel's brightness as an 8-bit unsigned binary number. A photographer notices that dark shadows sometimes appear as pure black (0) even when there is faint detail visible to the eye. Explain, referencing binary representation and bit limitations, why this occurs and propose one way to reduce the problem.
Show answer
Rubric: (1) The student identifies that 8-bit encoding limits brightness to 256 discrete levels (0–255), so very low-intensity light values are rounded down to 0, producing pure black. (2) The student explains that the spacing between consecutive representable values is too coarse to capture the subtle gradations present in shadow regions, resulting in quantization loss. (3) The student proposes a valid solution, such as using a higher bit depth (10-bit, 12-bit, or 16-bit encoding) to increase the number of discrete levels and thereby capture finer differences in low-light areas. Alternative valid solutions include applying a gamma curve that allocates more binary codes to darker values.

Question 5: CRITICAL THINKING

A protocol designer must choose how many bits to allocate for a field that stores the population of any country on Earth. As of 2024, the most populous country has approximately 1.4 billion people. (a) Determine the minimum number of bits required. Show your reasoning. (b) Explain why a designer might choose more bits than the theoretical minimum. (c) Discuss one trade-off of allocating extra bits. (d) If the designer chose 32 bits, calculate the maximum representable population and comment on whether this is sufficient for the foreseeable future.
Show answer
Rubric (4 points): (1) Part (a): The student must determine that 2³⁰ ≈ 1.07 billion is too small, while 2³¹ ≈ 2.15 billion is sufficient, so 31 bits are the minimum. Full credit requires showing or referencing the comparison of powers of 2 to 1.4 billion. (2) Part (b): The student explains practical reasons for over-allocation — future population growth, protocol longevity, alignment to standard data widths (e.g., 32 or 64 bits), or simplification of software implementation. (3) Part (c): The student identifies a trade-off: each extra bit doubles the range but also increases storage requirements per field. For a database with billions of records, even one extra byte per record can mean gigabytes of additional storage. Alternatively, bandwidth cost in transmission is a valid trade-off. (4) Part (d): 2³² − 1 = 4,294,967,295. Since no country currently exceeds 1.5 billion, a 32-bit field provides roughly 3× headroom. The student should note that 32 bits align with standard integer widths in most programming languages and hardware architectures, making this a practical and sufficient choice for the foreseeable future.

Varsity Tutors • AP Computer Science Principles • Binary Numbers