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:
255Four bits
printf("%u\n", max_value(4));Expected output:
15Learn it first
- Binary and hexadecimal · Bits, bytes, hex digits and negative numbers, the way hardware actually stores values.
More Binary and hexadecimal problems
- Value of a bit position · Compute how much each bit of a binary number is worth.
- Value of a hex digit · Convert a hexadecimal character into the number it stands for.
- Count the ones · Read off the binary digits of a number one at a time.
- How many bits? · Find the width needed to store a value.
- Split a byte into hex digits · Find the two hex digits of a byte with division and remainder.
- Read a byte as signed · Interpret the same eight bits as a negative number.