binary question

closed account (zwA4jE8b)
So I noticed that -122 in binary is 1000 0110 with leading 1's.

since 2^7 is 128 the h.o. nibble and the l.o. nibble is 6, does the computer just subtract 6 from 128 to get the 122?
Last edited on
Each bit has a weight of 2^n where n is the bit number

If the number is signed, the most significant bit has a negative weight

The value of the number is the sum of all bit weights, whose bit is set.

with 1000 0110, bits 1, 2, and 7 are set.

Therefore:

1
2
3
4
5
-(2^7) = -128  (negative because it is the most significant bit)
  2^2  =    4
  2^1  =    2
_____________
         -122
Last edited on
since 2^7 is 128 the h.o. nibble and the l.o. nibble is 6, does the computer just subtract 6 from 128 to get the 122?


So basically, yes. Nice explanation, Disch.
Thanks.

The nibble doesn't really have anything to do with it though, since only the high bit is signed... not the high nibble.
closed account (zwA4jE8b)
so 0101 is five but to sign it 1101 makes it -5 because the 1 is the most significant bit.

Thanks disch.
Last edited on
closed account (zwA4jE8b)
or is it because when you negate 5 you get 1010 then add 1 making it 1101. so when the one is added are all bits except the h.o. bit shifted left? because it looks like adding 1 to 1010 would be 1011.
so 0101 is five but to sign it 1101 makes it -5 because the 1 is the most significant bit.

No

The most significant bit still has a weight of 2^n -- it's just that it's weight is negative.

So if you have a signed 4 bit number: 1101

that is bits 0, 2, 3. so:

1
2
3
4
5
2^3 = -8  (negative because it is signed, MSB)
2^2 =  4
2^0 =  1
________
    = -3


-5 would be 1011 (bits 0,1,3)
1
2
3
4
5
2^3 = -8
2^1 =  2
2^0 =  1
________
    = -5



EDIT:

because it looks like adding 1 to 1010 would be 1011.


Right. 1011 would be -5.

Inverting all bits, then adding 1 is a quick way to negate a number in 2's compliment.
Last edited on
closed account (zwA4jE8b)
oh ok gotcha, I was looking at the chart on the wikipage link above and was looking in the wrong column when i got 1101 for -5..

Thank you, makes sense now.
Topic archived. No new replies allowed.