Working out how many bits a number needs

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Working out how many bits a number needs

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Can the number 20 be stored in 4 bits?
A No, 4 bits only go up to 15
B Yes, 4 bits go up to 20
2. How many bits does the number 25 need?
A 4 bits (largest value 15)
B 5 bits (largest value 31)
3. Which rule finds a big enough bit length for a number?
A Pick the smallest n where 2^n - 1 is at least the number
B Pick the n where 2^n is exactly equal to the number
4. What is the largest number 5 bits can hold?
A 31
B 32
5. You want to store the number 9. Which bit length is the smallest that is big enough?
A 3 bits, because 9 is close to 7
B 4 bits, because 3 bits only reach 7

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
12^4 - 1 = 15 A20 <= 2^4 - 1 is False
22^5 - 1 = 31 BForgot to subtract 1. Counting starts at 0, so the largest 4-bit number is 2^4 - 1 = 15.
3To test if a number fits in a bit length, compare it with the largest value for that length C4 bits can hold at most 15, so 20 is too big for 4 bits
4Choose the smallest bit length n whose largest value is at least your number Dn = 5 because 2^5 - 1 >= 25
5Largest 4-bit number = 2^4 = 16 E5 bits can hold up to 31, so 25 fits in 5 bits
631 needs 6 bits because 31 is not bigger than 2^5 - 1 F31 equals the largest 5-bit value (2^5 - 1 = 31), so it fits in 5 bits. A number only needs more bits if it is bigger than the maximum.

Definitions

2^n - 1 >= number

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
4 bits: 2^4 - 1 = __
2.
2^_ - 1 = 31, which is at least 25
3.
Is 2^5 - 1 __ 25? Yes, so 5 bits is enough
4.
n = 1 WHILE 2^n - 1 < 20: n = _____
5.
9: 2^3 - 1 = 7 is too small, so use 2^_ - 1

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A max_value = 2**4
B number = 20
C if number > max_value:
D print('Needs more than 4')
Question 2
A number = 25
B bits = 1
C while 2**bits - 1 < number:
D bits = bits - 1
E print(bits)
Question 3
A number = 25
B bits = 5
C largest = 2*bits - 1
D print(largest >= number)
Question 4
A number = 20
B largest_4 = 2**3 - 1
C print(number <= largest_4)
Question 5
A largest ← 5^2 - 1
B IF 25 <= largest THEN
C OUTPUT '5 bits is enough'
D ENDIF

Answer Key

Working out how many bits a number needs
Section A: Quick Questions
Q1
A — No, 4 bits only go up to 15
Q2
B — 5 bits (largest value 31)
Q3
A — Pick the smallest n where 2^n - 1 is at least the number
Q4
A — 31
Q5
B — 4 bits, because 3 bits only reach 7
Section B: Match the Pairs
Q1
2^4 - 1 = 15 → 4 bits can hold at most 15, so 20 is too big for 4 bits
Q2
2^5 - 1 = 31 → 5 bits can hold up to 31, so 25 fits in 5 bits
Q3
To test if a number fits in a bit length, compare it with the largest value for that length → 20 <= 2^4 - 1 is False
Q4
Choose the smallest bit length n whose largest value is at least your number → n = 5 because 2^5 - 1 >= 25
Q5
Largest 4-bit number = 2^4 = 16 → Forgot to subtract 1. Counting starts at 0, so the largest 4-bit number is 2^4 - 1 = 15.
Q6
31 needs 6 bits because 31 is not bigger than 2^5 - 1 → 31 equals the largest 5-bit value (2^5 - 1 = 31), so it fits in 5 bits. A number only needs more bits if it is bigger than the maximum.
Section C: Definitions
Q1
2^n - 1 >= number — Find the smallest number of bits n whose largest value, 2^n - 1, is at least your number. Example: 20 is too big for 4 bits (max 15), so it needs 5 bits (max 31).
Section D: Fill in the Blanks
Q1
4 bits: 2^4 - 1 = 15
Q2
2^5 - 1 = 31, which is at least 25
Q3
Is 2^5 - 1 >= 25? Yes, so 5 bits is enough
Q4
n = 1 WHILE 2^n - 1 < 20: n = n + 1
Q5
9: 2^3 - 1 = 7 is too small, so use 2^4 - 1
Section E: Spot the Error
Q1
A — The largest value n bits can hold is 2^n - 1, not 2^n. With 4 bits the largest value is 15, so the line should be max_value = 2**4 - 1.
Q2
D — While the largest value is still too small, we need MORE bits, so bits must go up by 1. Subtracting would never reach 5 bits for 25. It should be bits = bits + 1.
Q3
C — Each extra bit doubles the number of values, so we need a power: 2**bits. Using 2*bits gives 9 instead of 31. It should be largest = 2**bits - 1.
Q4
B — The variable is for 4 bits, so the power must be 4: 2**4 - 1 = 15. Using 2**3 - 1 gives the largest 3-bit value, which is 7.
Q5
A — The base must be 2 and the number of bits is the power. 5^2 - 1 is 24, but 5 bits hold up to 2^5 - 1 = 31. It should be largest ← 2^5 - 1.