A bit is a binary digit, a 0 or a 1, and a byte is 8 bits. In binary, each place is worth twice the place to its right (1, 2, 4, 8, …), so 1011 in binary is 8 + 2 + 1 = 11 in decimal. With n bits you can make 2ⁿ different patterns, so each extra bit doubles the number of values you can represent.
Quick check
What is 2 × 2 × 2 × 2?Show answerHide answer
- Four 2s multiplied together is 2⁴.
In the decimal number 450, what is the digit 4 worth?Show answerHide answer
400, because it is in the hundreds place.
How many different patterns can 2 bits make?Show answerHide answer
4: 00, 01, 10 and 11.
Know these already? Jump to the practice questions
What you'll be able to do
- State what a bit and a byte are.
- Convert a binary number to decimal using place values.
- Convert a decimal whole number to binary.
- Calculate how many values n bits can represent.
- Determine how many bits are needed for a given number of values.
Key terms
Bit
A binary digit: a single 0 or 1, the smallest unit of data a computer stores.
- In plain English
- One switch that is either off or on.
- Don't write
- "A bit is any single digit from 0 to 9."
Byte
A group of 8 bits.
- In plain English
- Eight bits handled as one unit.
- Don't write
- "A byte is 10 bits."
Binary
The base-2 number system, which uses only the digits 0 and 1; each place is worth twice the place to its right.
- In plain English
- Counting with only two digits.
- Don't write
- "1011 in binary means one thousand and eleven."
Why do computers use bits?
Computer hardware stores data in parts that are always in one of two clearly different states, such as a high or a low voltage. One state stands for 1 and the other for 0, so every piece of stored data is a pattern of bits.
Numbers, text, images and sound are all stored as bits. A program groups the bits and decides what they mean, so the same 8 bits could be a number in one program and part of a color in another. This is abstraction at work: a program handles numbers, letters and colors, while underneath they are all bits.
Memory and file sizes are counted in bytes. Binary place values, conversions and 2ⁿ are all "Learn this": the exam reference sheet covers the pseudocode, not binary.
Check yourself
Show answerHide answer
16, because each byte is 8 bits.
How does place value work in binary?
Decimal places are powers of 10: ones, tens, hundreds. Binary places are powers of 2:
| Power | 2⁷ | 2⁶ | 2⁵ | 2⁴ | 2³ | 2² | 2¹ | 2⁰ |
|---|---|---|---|---|---|---|---|---|
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
To convert binary to decimal, line the bits up under the place values from the right, then add the place values that have a 1 under them. For 1011, the 8, 2 and 1 places hold 1s, so the value is 8 + 2 + 1 = 11.
Two details matter. The rightmost bit is always the 1s place, so line the number up from the right. Zeros on the left change nothing: 00001011 is still 11.
Check yourself
Show answerHide answer
6: the 4 and 2 places hold 1s, and 4 + 2 = 6.
How do you convert decimal to binary?
Work from the largest place value down. At each place, if the place value fits into what is left, write 1 and subtract it; if not, write 0. Start at the largest place value that fits.
Converting 45:
| Place value | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|
| Fits into what is left? | 45: yes | 13: no | 13: yes | 5: yes | 1: no | 1: yes |
| Bit | 1 | 0 | 1 | 1 | 0 | 1 |
| Left after | 13 | 13 | 5 | 1 | 1 | 0 |
So 45 is 101101 in binary. Check by converting back: 32 + 8 + 4 + 1 = 45.
Every skipped place gets a 0. Leaving out a 0 shifts every bit to its left into the wrong place.
Check yourself
Show answerHide answer
1001: 8 fits (1 left), 4 and 2 do not, and 1 fits.
How many values can n bits represent?
Each bit has 2 possible values, so n bits make 2 × 2 × … × 2 = 2ⁿ different patterns.
| Bits | 1 | 2 | 3 | 4 | 8 |
|---|---|---|---|---|---|
| Patterns | 2 | 4 | 8 | 16 | 256 |
Adding one bit doubles the count. If the patterns stand for whole numbers starting at 0, n bits represent 0 to 2ⁿ − 1: the largest value is one less than the count, because 0 uses up a pattern. For example, an IPv4 Internet address is 32 bits long, so there are 2³² = 4,294,967,296 possible IPv4 addresses.
To find how many bits k different values need, find the smallest n with 2ⁿ ≥ k. For the 26 letters of the English alphabet, 2⁴ = 16 is too few and 2⁵ = 32 is enough, so you need 5 bits.
Key idea
n bits give 2ⁿ values; to store k values, find the smallest n with 2ⁿ ≥ k.
Check yourself
Show answerHide answer
7, because 2⁶ = 64 is too few and 2⁷ = 128 is enough.
Worked example 1: Binary to decimal
Question: What is 11010110 in binary as a decimal number?
What tells you the method
A binary number to convert, so use place values.
Step 1: Line up the bits under the place values.
Place value 128 64 32 16 8 4 2 1 Bit 1 1 0 1 0 1 1 0 WhyThere are 8 bits, so the leftmost bit is the 128 place.
Step 2: Add the place values with a 1.
128 + 64 + 16 + 4 + 2 = 214.
WhyA 0 contributes nothing to its place.
Check it
214 is even, and the 1s bit is 0, as it must be for an even number.
Answer
214
Worked example 2: Decimal to an 8-bit binary number
Question: Write 77 as an 8-bit binary number.
What tells you the method
A decimal number to convert, so subtract place values from the largest down.
Step 1: Find the largest place value that fits.
128 is too big; 64 fits, leaving 13.
WhyStarting too high or too low shifts every bit.
Step 2: Work down the remaining places.
32 and 16 do not fit into 13. 8 fits, leaving 5. 4 fits, leaving 1. 2 does not fit. 1 fits, leaving 0.
WhyEach skipped place gets a 0, so the bits stay in the right places.
Step 3: Write all 8 bits.
The 128 place is 0, then 64 to 1 give 1001101, so the 8-bit form is 01001101.
WhyAn 8-bit answer needs a bit in every place, including leading zeros.
Check it
64 + 8 + 4 + 1 = 77.
Answer
01001101
Worked example 3: Trace a bit-counting procedure
Question: What does bitsNeeded(200) return?
PROCEDURE bitsNeeded(count)
{
bits ← 0
values ← 1
REPEAT UNTIL(values ≥ count)
{
values ← values * 2
bits ← bits + 1
}
RETURN(bits)
}
What tells you the method
A REPEAT UNTIL loop, so trace it, checking the condition before each pass.
Step 1: Trace the loop.
valuesdoubles on each pass, so after pass n it is 2ⁿ.Pass bitsvaluesvalues ≥ 200?start 0 1 no 1 1 2 no 2 2 4 no 3 3 8 no 4 4 16 no 5 5 32 no 6 6 64 no 7 7 128 no 8 8 256 yes WhyREPEAT UNTILstops as soon as the condition is true (on the exam reference sheet).Step 2: Read the result.
The loop stops with
bitsat 8, so the procedure returns 8.WhyIt finds the smallest n with 2ⁿ ≥ 200, which is the number of bits 200 values need.
Check it
Try the boundary bitsNeeded(128): after pass 7, values is 128 and 128 ≥ 128 is true, so it returns 7, and 7 bits do give exactly 128 values.
Answer
8
Your turn
Question: The list holds the bits of a binary number, left to right. What is displayed?
bits ← [1, 0, 1, 1, 0]
value ← 0
FOR EACH b IN bits
{
value ← value * 2 + b
}
DISPLAY(value)
Step 1: Pass 1.
valueis 0 × 2 + 1 = 1.Step 2: Pass 2.
valueis 1 × 2 + 0 = 2.Step 3: Your go: finish passes 3 to 5 and give what is displayed.
Show the full solutionHide the solution
Step 3. Pass 3: 2 × 2 + 1 = 5. Pass 4: 5 × 2 + 1 = 11. Pass 5: 11 × 2 + 0 = 22. Multiplication comes before addition, and doubling shifts every earlier bit one place left. Check: 10110 in binary is 16 + 4 + 2 = 22.
Answer: 22
Traps
Trap
Binary place values go up by 1 or by 10 for each place.
It seems right because decimal places go up by a factor of 10. But binary places double: 1, 2, 4, 8, 16, so 100 in binary is 4, not 100 or 3.
What is 1000 in binary as a decimal number?
Trap
n bits can store values up to 2ⁿ.
It seems right because n bits make 2ⁿ patterns. But one pattern is used for 0, so whole numbers from 0 reach only 2ⁿ − 1.
What is the largest whole number 4 bits can represent, starting from 0?
15, which is 1111 in binary; 2⁴ = 16 counts the patterns.
What's examined
On the AP® Computer Science Principles exam, binary belongs to Big Idea 2: Data. Multiple-choice questions ask you to convert between binary and decimal, compare binary values, and work out how many values a number of bits can represent or how many bits a set of values needs. Practice the conversions by hand until they are quick. The notes on Overflow, Round-Off and Analog Data and on Lossless and Lossy Compression build on this.
Practice questions
- Recall How many bits are in 3 bytes?
Show answer
Answer: 24
Marking points:
- A byte is 8 bits, and 3 × 8 = 24.
- Apply What is 101010 in binary as a decimal number?
Show answer
Answer: 42
Marking points:
- The places from the right are 1, 2, 4, 8, 16 and 32.
- The 1s are in the 32, 8 and 2 places: 32 + 8 + 2 = 42.
- Apply What is 19 in decimal written in binary?
- A) 10010
- B) 10011
- C) 11001
- D) 10101
Show answer
Answer: B. 19 = 16 + 2 + 1, so the 16, 2 and 1 places hold 1s: 10011.
Why the others are wrong:
- A is 18: the 1s place was left out.
- C is 25: the correct bits written in reverse order.
- D is 21: 16 + 4 + 1, using the 4 place instead of the 2 place.
- Apply What is displayed when this code segment runs?
total ← 0
place ← 1
REPEAT 5 TIMES
{
total ← total + place
place ← place * 2
}
DISPLAY(total)
Show answer
Answer: 31
Marking points:
- The loop adds
placebefore doubling it, so it adds 1, 2, 4, 8 and 16. - 1 + 2 + 4 + 8 + 16 = 31, which is 11111 in binary, the largest 5-bit value.
- Exam-style A school gives each of its 1,500 students a different ID number in binary, using the same number of bits for every ID. What is the fewest bits that gives every student a different ID?
- A) 8
- B) 10
- C) 11
- D) 12
Show answer
Answer: C. 2¹⁰ = 1,024 is fewer than 1,500, and 2¹¹ = 2,048 is enough.
Why the others are wrong:
- A assumes one byte is enough, but 2⁸ = 256.
- B stops one power too soon: 1,024 IDs cannot cover 1,500 students.
- D works, since 2¹² = 4,096, but it is not the fewest.
- Exam-style Which 8-bit binary number has the greatest value?
- A) 01111111
- B) 10000000
- C) 01101110
- D) 10000001
Show answer
Answer: D. It is 128 + 1 = 129.
Why the others are wrong:
- A has the most 1s, but its 128 place is 0, so it is only 127.
- B is 128, one less than D.
- C is 64 + 32 + 8 + 4 + 2 = 110.
- Exam-style A sensor stores each reading in 7 bits. The design changes to 8 bits. How does the number of different readings it can store change?
- A) It doubles, from 128 to 256.
- B) It goes up by 1, from 7 to 8.
- C) It goes up by 8, from 128 to 136.
- D) It goes up by 1, from 127 to 128.
Show answer
Answer: A. 2⁷ = 128 and 2⁸ = 256: each extra bit doubles the count.
Why the others are wrong:
- B counts bits, not values.
- C adds the number of bits to the count instead of doubling it.
- D adds 1 to the largest value instead of doubling the count.
Key points
Recall, then reveal
Say the main points from memory first, then check them.
Show key pointsHide key points
- A bit is a 0 or a 1; 8 bits make a byte.
- Binary place values from the right are 1, 2, 4, 8, 16, 32, 64, 128.
- Binary to decimal: add the place values holding 1s. Decimal to binary: subtract place values from the largest that fits.
- n bits make 2ⁿ patterns; as whole numbers from 0 they reach 2ⁿ − 1.
- k values need the smallest n with 2ⁿ ≥ k bits.