Computer Science
Basic

Bits to Represent n

Minimum number of bits to represent n distinct values.

Formula

b=⌈log⁡2(n)⌉b = \lceil \log_2(n) \rceil

Variables

nNumber of values

Example

To label 1000 items you need ⌈log₂ 1000⌉ = 10 bits.

How it works

Each bit doubles the number of distinct patterns you can make, so 1 bit gives 2 possibilities, 2 bits give 4, 3 bits give 8, and so on. To represent at least n different values, you need enough bits so that 2^b is at least n; solving for b gives b = ceil(log2(n)). The ceiling is important because you cannot use a fraction of a bit, so you round up to the next whole number.

When to use it

Use this when figuring out how many bits are needed to encode IDs, labels, states, or any set of distinct values in a computer system. It is common in data encoding, digital design, networking, and memory sizing.

Worked example, step by step

Suppose you need to assign unique codes to 37 different sensor readings. Step 1: Write the formula b = ceil(log2(n)) Step 2: Substitute n = 37, so b = ceil(log2(37)) Step 3: Compute log2(37) ≈ 5.21 Step 4: Round up to the next whole number: b = 6 Final answer: 6 bits are needed, because 2^6 = 64 possible codes, which is enough for 37 values.

Common mistakes to avoid

  • Forgetting to round up and using the decimal value of log2(n) as the answer
  • Using log10 instead of log base 2
  • Mixing up the number of values with the number of bits, especially for powers of 2

Did You Know?

Just 33 bits can uniquely label every person on Earth; 64 bits can count grains of sand on all our beaches.

Share this formula

Reviewed & maintained by the FormulaWon editorial teamLast reviewed September 2026