Value of a bit position
Compute how much each bit of a binary number is worth. An easy embedded C problem (Binary and hexadecimal), run and graded in the browser.
easy · Binary and hexadecimal · binary
In binary, each position is worth twice the one before it. Bit 0 is worth 1, bit 1 is worth 2, bit 2 is worth 4, and so on.
Return the value of bit n, which is 2 multiplied by itself n times. n is between 0 and 31.
Starter code
unsigned power_of_two(unsigned n) {
unsigned value = 1;
// Double value, n times.
return value;
}Example tests
Bit 4
printf("%u\n", power_of_two(4));Expected output:
16Bit 1
printf("%u\n", power_of_two(1));Expected output:
2Learn it first
- Binary and hexadecimal · Bits, bytes, hex digits and negative numbers, the way hardware actually stores values.
More Binary and hexadecimal problems
- Largest value in n bits · Work out the range of an unsigned number from its width.
- 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.