- Practice
- About 13 min
- 5 practice questions
Bits, bytes and how many values fit
Find how many values n bits can represent and how many bits are needed for a given count of items.
If something looks wrong, please tell us.
A common question type asks how many different values can be stored in a given number of bits, or how many bits are needed to store a given number of items. Both rely on powers of 2.
The key facts
- With n bits you can make 2^n different patterns.
- If numbers start from 0, the largest value in n bits is 2^n - 1.
- To store N different items, find the smallest n such that 2^n is at least N.
| n | 2^n |
|---|---|
| 1 | 2 |
| 2 | 4 |
| 3 | 8 |
| 4 | 16 |
| 5 | 32 |
| 6 | 64 |
| 7 | 128 |
| 8 | 256 |
| 10 | 1024 |
Worked examples
Example 1. How many values can 8 bits hold, and what is the largest unsigned number? 2^8 = 256 values, ranging from 0 to 255.
Example 2. How many bits are needed to give a different code to each of 50 items? 2^5 = 32 is too small; 2^6 = 64 is at least 50. So 6 bits.
Example 3. A colour is stored with 3 bits for each of red, green and blue. How many colours? Total bits = 9, so 2^9 = 512 colours.
n bits give 2^n patterns. The largest unsigned value is 2^n - 1.
Key takeaways: use 2^n, subtract 1 for the largest value and find the smallest power of 2 that fits.
Check your understanding
-
1. How many different values can 6 bits represent?
- 6
- 12
- 32
- 64
Show answer
Answer: D. n bits give 2^n values: 2^6 = 64.
-
2. What is the largest unsigned number that fits in 7 bits?
- 127
- 128
- 64
- 255
Show answer
Answer: A. The largest unsigned value in n bits is 2^n - 1 = 128 - 1 = 127.
-
3. How many bits are needed to give a different code to each of 20 items?
- 4
- 5
- 6
- 20
Show answer
Answer: B. 2^4 = 16 is too few; 2^5 = 32 is enough, so 5 bits.
-
4. A colour uses 2 bits for each of red, green and blue. How many colours are possible?
- 6
- 12
- 64
- 8
Show answer
Answer: C. Total bits = 3 x 2 = 6, and 2^6 = 64 colours (also 4 x 4 x 4 = 64).
-
5. How many different values can 2 bytes hold?
- 65,535
- 256
- 512
- 65,536
Show answer
Answer: D. 2 bytes = 16 bits, and 2^16 = 65,536. (65,535 is the largest unsigned value, not the count.)
Sign in to keep track of the lessons you finish.