How many bits do you need to store a positive integer?

Yes. the maximal number stored in k bits is 2^k-1, since there is 2^k options for the bits, and one of them is zero.
Therefore, the number of bits required to store a number N is log2(N), but since there is no half bit, you need to round it up to the cloest integer above.

Note: if you need to include negative numbers, there must be one more bit for the sign.


Since I've seen the answer reported incorrectly so many times, I thought I would post the correct answer.

The number of bits needed to represent the positive integer n is

bits = floor( log2(n) + 1 )

where log2 means log base 2.

Tags:

Integer

Binary