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:
5All bits set
printf("%d\n", as_signed_byte(255));Expected output:
-1Learn 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.
- 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.