Simvik

Read a byte as signed

Interpret the same eight bits as a negative number. An easy embedded C problem (Binary and hexadecimal), run and graded in the browser.

easy · Binary and hexadecimal · twos-complement

Eight bits can be read as 0 to 255, or as a signed number from -128 to 127. In the signed reading, called two's complement, the values 128 to 255 stand for -128 to -1.

Given a byte value raw from 0 to 255, return what it means as a signed 8-bit number.

Starter code

int as_signed_byte(int raw) {
  return raw;
}

Example tests

A small positive value

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

Expected output:

5

All bits set

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

Expected output:

-1

Learn it first

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

More Binary and hexadecimal problems