Simvik

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:

16

Bit 1

printf("%u\n", power_of_two(1));

Expected output:

2

Learn it first

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

More Binary and hexadecimal problems