01 / 21
0 1 1 0

Binary Numbers
The Language of Computers

Learn how computers represent all data using just two digits: 0 and 1. Master conversions between binary and decimal systems.

AP Computer Science Principles

What is Binary?

Binary is a base-2 number system that uses only two digits: 0 and 1. Every piece of data inside a computer: numbers, text, images, audio, and video: is ultimately stored as a sequence of binary digits, called bits.

Bit
The smallest unit of data: a single 0 or 1
Byte
8 bits grouped together, e.g. 01101001

Why Do Computers Use Binary?

Computer hardware is built from billions of tiny transistors: electronic switches that are either on or off. Binary maps perfectly to this physical reality.

OFF 0 → → ON 1 1 0 1 1 Binary Data

Decimal vs Binary Systems

The decimal system you use every day is base-10: it uses digits 0 through 9, and each place value is a power of 10. Binary works the same way, but each place value is a power of 2.

Decimal (Base-10)
Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Place values: \(\ldots, 10^2, 10^1, 10^0\)
Binary (Base-2)
Digits: 0, 1
Place values: \(\ldots, 2^2, 2^1, 2^0\)

The Place Value System

Each position in a binary number corresponds to a power of 2. The rightmost position is \(2^0 = 1\), and each position to the left doubles in value.

128 \(2^7\) 64 \(2^6\) 32 \(2^5\) 16 \(2^4\) 8 \(2^3\) 4 \(2^2\) 2 \(2^1\) 1 \(2^0\) ← Most Significant Bit (MSB) Least Significant Bit (LSB) →

Binary to Decimal Conversion

To convert a binary number to decimal, multiply each bit by its place value and add the results together.

Example: 1011 in binary

= 1 × \(2^3\) + 0 × \(2^2\) + 1 × \(2^1\) + 1 × \(2^0\)
= 8 + 0 + 2 + 1
= 11 in decimal

Decimal to Binary Conversion

To convert a decimal number to binary, repeatedly divide by 2 and record the remainders. Read the remainders from bottom to top.

1
Divide the decimal number by 2
2
Record the remainder (0 or 1)
3
Repeat with the quotient until it reaches 0
4
Read remainders bottom-to-top for the binary result

Comparing Number Systems

Property Decimal Binary Hexadecimal
Base 10 2 16
Digits Used 0 – 9 0 – 1 0 – 9, A – F
Example: 255 255 11111111 FF
Used By Humans Computers Programmers
Place Values \(10^n\) \(2^n\) \(16^n\)

Step-by-Step Conversion Checklist

Binary → Decimal
1. Write the place values above each bit
2. Multiply each bit by its place value
3. Add all the products together
Decimal → Binary
1. Divide by 2, write the remainder
2. Divide quotient by 2, write remainder
3. Repeat until quotient is 0
4. Read remainders bottom-to-top

Example 1: Convert 10110 to Decimal

\(2^4\)
1
16
\(2^3\)
0
0
\(2^2\)
1
4
\(2^1\)
1
2
\(2^0\)
0
0
1 × 16 + 0 × 8 + 1 × 4 + 1 × 2 + 0 × 1
= 16 + 0 + 4 + 2 + 0
= 22

Example 2: Convert 45 to Binary

45 ÷ 2 = 22 remainder 1 ← LSB
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 ← MSB
\(2^5\)
1
32
\(2^4\)
0
0
\(2^3\)
1
8
\(2^2\)
1
4
\(2^1\)
0
0
\(2^0\)
1
1

Example 3: Convert 11001010 to Decimal

\(2^7\)
1
128
\(2^6\)
1
64
\(2^5\)
0
0
\(2^4\)
0
0
\(2^3\)
1
8
\(2^2\)
0
0
\(2^1\)
1
2
\(2^0\)
0
0
128 + 64 + 0 + 0 + 8 + 0 + 2 + 0 = 202

Binary Addition

Binary addition follows the same rules as decimal addition, but carrying happens at 2 instead of 10.

Addition Rules
\(0 + 0 = 0\)
\(0 + 1 = 1\)
\(1 + 0 = 1\)
\(1 + 1 = 10\) (write 0, carry 1)
Example
  1011 (11)
+ 0110 (6)
————
10001 (17)

Understanding Overflow

When a binary number exceeds the available number of bits, an overflow occurs. The extra bit is lost, producing an incorrect result.

4-bit max value
\(1111 = 15\)
\(+1\)
Overflow! Extra bit lost
\(10000 \rightarrow 0\)
\(15 + 1\) should be \(16\), but with only \(4\) bits it wraps to \(0\)
This is why computers have fixed bit-widths (8, 16, 32, 64 bits)

Common Mistakes to Avoid

✕ Reading Remainders Top-Down
When converting decimal to binary, remainders must be read bottom to top. Reading top-down reverses the number.
✕ Wrong Place Values
Using powers of 10 instead of powers of 2. Remember: binary positions are \(1, 2, 4, 8, 16, \ldots\) not \(1, 10, 100, \ldots\)
✕ Forgetting Leading Zeros
Binary 0011 and 11 are the same value. Leading zeros don't change the number, but matter for fixed bit-widths.

Powers of 2 Reference Table

Power Decimal Value Binary Bits Needed
\(2^0\) 1 1 1
\(2^1\) 2 10 2
\(2^2\) 4 100 3
\(2^3\) 8 1000 4
\(2^4\) 16 10000 5
\(2^7\) 128 10000000 8
\(2^8\) 256 100000000 9
\(2^{10}\) 1024 10000000000 11

Question 1 of 5

What is the decimal value of the binary number 1101?

Question 2 of 5

What is 42 in binary?

Question 3 of 5

What is the maximum decimal value that can be represented with 6 bits?

Question 4 of 5

What is the result of adding the binary numbers 1011 and 0101?

Question 5 of 5

A computer uses 4 bits to store an unsigned integer. If the current value is 1111 and you add 1, what happens?