Question

In c ~ is 1's complement operator. This is equivalent to: ~a = -b + 1 So, a - ~b -1 = a-(-b + 1) + 1 = a + b – 1 + 1 = a + b

Can anyone explains this to me?

Was it helpful?

Solution

From elementary school math we know

a = -(-a);

From twos complement we know that

-a = (~a) + 1  (invert and add one)

so we know that

a + b 
= a - (-b)      elementary math
= a - (~b + 1)  twos complement
= a - (~b) - 1   distribute the negative (elementary math)

OTHER TIPS

You are right that ~ is always 1's complement (aka bitwise not) in c. Where you are going wrong is this: C does not guarantee 2's complement for numbers. So all your calculations depend on using a major flavor of C.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top