Bit manipulation
integer representation
binary systems
computer science
algorithms

Minimum number of bits to represent a given int

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The minimum number of bits needed to represent an integer depends on what representation you mean. A non-negative integer stored as an unsigned value uses one rule, while a signed integer stored in two's complement uses another.

Unsigned Case

For a non-negative integer n, the minimum number of bits is:

  • '1 if n == 0'
  • otherwise floor(log2(n)) + 1

That is just the position of the highest set bit.

Examples:

  • '0 needs 1 bit'
  • '1 needs 1 bit'
  • '2 needs 2 bits'
  • '7 needs 3 bits'
  • '8 needs 4 bits'

Python makes this easy with bit_length():

python
1def unsigned_bits(n):
2    if n < 0:
3        raise ValueError("expected non-negative integer")
4    return max(1, n.bit_length())
5
6
7for value in [0, 1, 2, 7, 8, 255]:
8    print(value, unsigned_bits(value))

Output:

text
10 1
21 1
32 2
47 3
58 4
6255 8

Signed Two's Complement Case

For signed storage, the smallest width b must satisfy this range:

  • minimum: -2^(b - 1)
  • maximum: 2^(b - 1) - 1

So you need the smallest b whose signed range contains the value.

Examples:

  • '0 fits in 1 bit'
  • '1 needs 2 bits'
  • '-1 fits in 1 bit'
  • '2 needs 3 bits'
  • '-2 fits in 2 bits'

That is why signed width is not always "unsigned bits plus one."

Signed Example in Python

python
1def signed_bits(n):
2    bits = 1
3    while n < -(1 << (bits - 1)) or n > (1 << (bits - 1)) - 1:
4        bits += 1
5    return bits
6
7
8for value in [-3, -2, -1, 0, 1, 2, 3]:
9    print(value, signed_bits(value))

Output:

text
1-3 3
2-2 2
3-1 1
40 1
51 2
62 3
73 3

A Simple C Example

For positive values in C, you can count right shifts:

c
1#include <stdio.h>
2
3int unsigned_bits(unsigned int n) {
4    int bits = 1;
5    while (n > 1) {
6        n >>= 1;
7        bits++;
8    }
9    return bits;
10}
11
12int main(void) {
13    printf("%d\n", unsigned_bits(10));
14    return 0;
15}

This prints 4 because 10 is 1010 in binary.

Why the Definition Matters

People often ask this question without saying whether negative numbers are possible. That missing detail changes the answer completely. Compression and bit packing usually care about unsigned magnitude. Machine integer widths and file formats may care about signed two's complement ranges instead.

There is also a difference between writing down a number in binary and storing it in a machine field. The shortest mathematical representation of a magnitude is not automatically the same as the smallest signed storage width that still preserves correct arithmetic behavior.

That is why interview questions and systems questions sometimes sound similar but expect different answers. A bit-packing problem may want magnitude only, while a compiler or protocol question may require a signed storage range.

Common Pitfalls

  • Forgetting to specify signed versus unsigned representation.
  • Claiming zero needs zero bits in stored form.
  • Assuming signed width is always unsigned width plus one.
  • Using floating-point log2 when direct bit operations are simpler.
  • Mixing mathematical binary notation with a machine storage format.

Summary

  • For non-negative integers, the answer comes from the highest set bit.
  • For signed integers, use the smallest two's complement width that contains the value.
  • Zero is typically treated as needing one bit in minimal storage.
  • Signedness changes the result, so define the representation first.
  • Bit operations are usually the safest way to compute the width in code.

Course illustration
Course illustration

All Rights Reserved.