Jonas Grønbek
Jonas Grønbek

Reputation: 2019

-3 + -3 = -2 in two's complement

using 4-bit numbers calculating -3 + -3 in two's complement, my calculation yields -2.

3 in binary is 0011

making both one's complement by flipping all the digits since both are negative

1100 && 1100

adding one to them, since we are using two's complement

1101 + 1101 =

11010

first one is overflow, and are tossed away in two's complement. So are left with 1010, which is minus two in decimal. Can someone explain what is done wrong in this process?


EDIT

My problem seems more specifically how to interpret the result of a two's complement calculation. I treat the result as following:

The result is 1010. In my world, the first bit is a sign bit, indicating that the number is negative. The following 0 means there is 0 of -4's the following 1 means there is 1 of -2's the following 0 means there is 0 of -1's

therefore, I interpret it like the result is -2

Upvotes: 1

Views: 652

Answers (2)

dbush
dbush

Reputation: 223689

The result is correct, you just interpreted it wrong. When you perform arithmetic on numbers in two's complement, the result is also two's complement. So the conversion from negative to positive is done the same way as the conversion of the original numbers from positive to negative.

Given the 4-bit binary value 1010, first flip the bits for one's complement to get 0101, then add 1 for two's complement to get 0110.

0110 is 6, so 1010 is -6.

Upvotes: 1

Govind Parmar
Govind Parmar

Reputation: 21532

first one is overflow

If, as you imply here, you are restricting your storage to a single nybble, then with two's complement, you can represent values in the range -8 to 7.

-2 would then be 0b1110, which is not what you have. -6 is indeed 0b1010, which is the correct sum.

Upvotes: 0

Related Questions