Simvik

How many bits?

Find the width needed to store a value. An easy embedded C problem (Binary and hexadecimal), run and graded in the browser.

easy · Binary and hexadecimal · binary · ranges

Return the smallest number of binary digits that can hold n. The value 5 is 101, which needs 3 bits. Zero needs 1 bit.

Starter code

int bits_needed(unsigned n) {
  return 0;
}

Example tests

Five

printf("%d\n", bits_needed(5));

Expected output:

3

The largest byte value

printf("%d\n", bits_needed(255));

Expected output:

8

Learn it first

  • Binary and hexadecimal · Bits, bytes, hex digits and negative numbers, the way hardware actually stores values.

More Binary and hexadecimal problems