Why does -2 equal to 1111 1110?

2014-05-18 8:43 pm
For a byte, positive 2 should be equal to 0000 0010.

I read from somewhere that for a signed number, the most significant bit will be used to determine whether the number is positive or negative.

So why doesn't -2 equal to 1000 0010?

I already checked in calculator, -2 equals to 1111 1110.

Thanks in advance.

回答 (5)

2014-05-18 10:09 pm
✔ 最佳答案
Two's compliment.
0000 0010 = 2
1111 1101 = one's compliment (all bits inverted)
1111 1110 = two's compliment (add 1) = -2

Now, WHY do that?

So a CPU can add negative and positive numbers easily
-2 + 8 = 6
1111 1110 = -2 (two's compliment)
0000 1000 = 8
0000 0110 = 6

Also two's compliment is used for subtraction because a CPU doesnt actually subtract, it adds.

9 - 2 = 7
0000 1001 = 9
1111 1110 = 2 using two's compliment, then add (not subtract)
0000 0111 = 7
2014-05-19 5:39 am
So why doesn't -2 equal to 1000 0010?
it does in sign magnitude.
but there are two zeros
1000 0000
and
0000 0000

in twos complement it is
1111110

twos complement makes machine math easier.
2014-05-18 10:32 pm
Perhaps it's easier to imagine how 2's complement works by thinking of 10's complement instead. Imagine an old style cassette deck with a mechanical counter. We're halfway through the tape and zero the counter.

Now, go forward and watch the counter:
0000
0001
0002
0003

Now rewind and watch the counter:
0003
0002
0001
0000
9999 = -1
9998 = -2
9997 = -3

It's quite obvious that 9999 should be -1, 9998 should be -2 and so on.

Well, the same rules apply in binary, but instead of going 0000->9999 as in decimal, we go 0000->1111, then 1110, then 1101, then 1100 and so on.

So, 1111 = -1, 1110 = -2, 1101 = -3, 1100 = -4, etc
2014-05-18 8:55 pm
There are multiple ways one can use bits to store a negative number. The way you're talking is called "sign and magnitude" representation - where the MSB is used to store whether or not the number is negative.

However, the most common way used in computers these days is called two's complement representation. That's why -2 is 1111110 in the calculator, because it uses two's complement, not sign and magnitude.

See http://en.wikipedia.org/wiki/Sign-and-magnitude#Signed_magnitude_representation
2014-05-18 8:47 pm
Because that isn't how negative numbers in binary are stored.

http://en.wikipedia.org/wiki/Two%27s_complement

The more in depth reason is that it makes math with negative numbers easier/cheaper to implement.


收錄日期: 2021-04-18 15:35:41
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20140518124325AAy7BWg

檢視 Wayback Machine 備份