Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2's Complement Error

I generally understand the concept of 2's complement. When converting a value to 2's complement for subtraction, simply negate each digit of the number to get 1's complement and add 1. When calculating the result of something like 2-2 in binary, you first convert -2 to 2's complement, (I like to use 4 bit representations) 0010 is turned into 1101 and then into 1110. Then you evaluate 0010 + 1110 and get 0000 and the overflowed 1 is truncated.

However, what about a problem like 2-3? So I did the same operation. 2 is 0010 and the 2's complement of 3 (0011) is 1101. So 0010 + 1101 results in 1111 and no overflow. The result is 15. 15 is not negative 1. Am I missing a crucial point about how 2's complement works that other websites and sources neglect to mention?

like image 997
Orren Ravid Avatar asked Aug 24 '26 18:08

Orren Ravid


1 Answers

In 2's compliment 1111 is -1 (if you only have 4 bits of course)

From 1111 to its absolute value invert:

0000

and add 1

0001

like image 63
Andrew Avatar answered Aug 28 '26 15:08

Andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!