Reputation: 5123
So in twos complement, -1 is represented as 11111111 as an 8-bit number.
so how does
11111111
11111111+
_________
11111110
Upvotes: 1
Views: 411
Reputation: 21
The same as adding positive numbers!
1 + 1 = 10, right? So the true addition is:
11111111
11111111+
_________
111111110
but we're using fixed-length 8-bit arithmetic, so the leftmost bit is lost, giving the answer
11111110
or -2.
Another way to look at this is that
bit 0 has value 1
bit 1 has value 2
bit 2 has value 4
..
bit 6 has value 64
bit 7 has value -128 (this is 'the sign bit')
Upvotes: 2
Reputation: 29680
You'd add it just like you would any other value. The addition of the two, least significant bits result in overflow, and the bit is carried over to be added to the second, least significant bit, and so on. Once the two, most significant bits are added, the bit that is carried over (due to overflow) is simply discarded.
Upvotes: 1