AP COMPUTER SCIENCE PRINCIPLES • DATA

Binary Numbers

Understanding how computers represent all data using only two digits: 0 and 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.

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.
KEY TAKEAWAY
KEY TAKEAWAY

Visual Explanation — Place Value Diagram

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.

Mathematical Framework

BINARY TO DECIMAL CONVERSION
V = 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 ≤ 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)
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.

Detailed Breakdown — Conversion Methods

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.

Common decimal-to-binary reference values
DecimalBinaryBit Count
001
71113
1511114
16100005
255111111118
2561000000009

Worked Example — Binary Addition & Overflow

1
Step 1 — Interpret the operandsConvert 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
Step 2 — Align and add column by columnStarting 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
Step 3 — Record the resultThe 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
Step 4 — Detect overflowBecause 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

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
KEY TAKEAWAY
KEY TAKEAWAY

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.

Practice Problems

1
Which of the following best explains why computers use binary (base-2) to represent data?
2
What is the decimal value of the binary number 11010110₂?
3
A computer uses 4-bit unsigned binary to store values. Which TWO of the following statements are true about this system?
PROBLEM 4APPLIED
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.
PROBLEM 5CRITICAL 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.
Varsity Tutors • AP Computer Science Principles • Binary Numbers