[Math] Represent the following decimal values in 8-bit 2’s complement and hexadecimal

algebra-precalculusbinaryintuition

-27: 11100101

-128: 10000000

I'm having trouble coming up with the hexadecimal representation. Does it change if my numbers are represented in 2's complement as opposed to just regular binary? Also, why does an 8-bit 2's complement range from 127 to -128? What number is represented by 11111111 (if this is allowed, but I'm not sure why it wouldn't be)?

Thank you.

Best Answer

This depends on what kind of hexadecimal representation you're looking for; if you just want the numbers in base $16$ with the sign, drop the sign and convert them. If you want to convert the binary numbers to base $16$, use the following neat trick:

Since $16 = 2^4$, every four bits correspond to a single hexadecimal digit. So what you should do is pad the number with zeros (say you had $1110101110$, take $001110101110$), then group it into four digit groups ($0011~1010~1110$), then use the following table to translate:

$$ 0000 \to 0 $$ $$ 0001 \to 1 $$ $$ 0010 \to 2 $$ $$ 0011 \to 3 $$ $$ 0100 \to 4 $$ $$ 0101 \to 5 $$ $$ 0110 \to 6 $$ $$ 0111 \to 7 $$ $$ 1000 \to 8 $$ $$ 1001 \to 9 $$ $$ 1010 \to A $$ $$ 1011 \to B $$ $$ 1100 \to C $$ $$ 1101 \to D $$ $$ 1110 \to E $$ $$ 1111 \to F $$

For example, $0011~1010~1110 \to 3AE$

As for two's complement, the rule is $\tilde{} n = -n -1$, that is, for $8$ digit numbers $11111111$ represents $0-1 = -1$. Every number with a leading $1$ is negative, so the smallest is $10000000 = \tilde{} 01111111 = -127 - 1 = -128 $, and the largest is $01111111 = 127$.

Related Question