Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

-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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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$.

share|improve this answer
    
Does this make -27 = 0x1B and -128 = 0x80? –  Bob John Jan 20 '13 at 22:39
    
-128 = 0x80, but 0x1B = 27; your previous computation shows that -27 = 0xE5. –  Alfonso Fernandez Jan 20 '13 at 22:46

In two's complement, the highest order bit is the negative of its usual value. For example, in $8$ bit two's complement $\color{#C00000}{1}\color{#00A000}{011\ 0100}=\color{#C00000}{-128}+\color{#00A000}{52}=-76$.

By changing the meaning of the bit that means $128$ to $-128$, the number is unchanged $\bmod\ 2^8$.

The conversion from binary to hexadecimal is the same as usual: $1011\ 0100=0\mathrm{xB}4$

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.