Simvik

Largest value in n bits

Work out the range of an unsigned number from its width. An easy embedded C problem (Binary and hexadecimal), run and graded in the browser.

easy · Binary and hexadecimal · binary · ranges

With bits binary digits, all set to 1, you get the largest number that width can hold. Eight bits give 255, and sixteen give 65535.

Return the largest unsigned value that fits in bits bits. bits is between 1 and 32.

Starter code

unsigned max_value(unsigned bits) {
  return 0;
}

Example tests

A byte

printf("%u\n", max_value(8));

Expected output:

255

Four bits

printf("%u\n", max_value(4));

Expected output:

15

Learn it first

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

More Binary and hexadecimal problems