Understanding the Effects of 2's Complement on Arithmetic Operations

AI Thread Summary
The discussion revolves around the confusion surrounding arithmetic operations using 2's complement, particularly why results appear to differ by two. It highlights specific examples where the addition of negative and positive numbers yields unexpected results, such as -62 instead of -64 and 33 instead of 35. Participants clarify the correct method for converting negative numbers to 2's complement, emphasizing the need to invert all bits and add one, rather than leaving the last bit unchanged. The importance of understanding the most significant bit (MSB) in determining the sign of the result is also noted. Overall, the conversation aims to rectify misconceptions about the 2's complement arithmetic process.
geft
Messages
144
Reaction score
0
Why is it that when using 2's complement, the result of arithmetic operations differ by two?

11011001 (-39) +
11100111 (-25) =
11000000 (which is -62 in 2's complement, even though it's supposed to be -64)

00110011 (51) +
11101110 (-16) =
00100001 (33, but it's supposed to be 35)
 
Physics news on Phys.org
Some errors:

-64 is 11000000 in 2's complement.

-16 is 11110000 in 2's complement.
 
64 is 11000000 in 2's complement.

Am I doing it wrong?
11000000
=> 00111110
= 2^1 + 2^2 + 2^3 + 2^4 + 2^5 = 62
=> -62

16 is 11110000 in 2's complement.

11110000
=> 00001110
= 2^1 + 2^2 + 2^3 = 14
=> -14
 
geft said:
Am I doing it wrong?
11000000
=> 00111110
Looks like you're subtracting one after inverting the bits? I believe you always add one, so it should be
00111111 + 1 = 01000000

and
11110000
=> 00001110
00001111 + 1 = 00010000
 
Agree with eumyang.

The algorithm for dealing with negative numbers (positives are easy--do nothing) is this :

To convert, say -64 into 8-bit 2's complement:
1) Take the 8-bit binary of the positive part (64): 0100 0000
2) Since the original value is negative, complement the result from 1) and add 1: 1011 1111 + 1 = 1100 0000


To find the decimal given the 8-bit 2's complement value, say for 1100 0000:
1) Note that the MSB is a 1 and therefore your result will be a negative number.
2) Complement the original value: 1100 0000 => 0011 1111
3) Add 1: 0011 1111 + 1 = 0100 0000 (which is binary 64)
4) Apply the negative sign from part 1) to the result from part 3) "-" : 64 = -64
 
I see where I was wrong... I thought I didn't need to change the last bit in any way, so when inverting I always leave the last bit alone. Thanks!
 
Back
Top