user238795
user238795

Reputation: 45

How does ~0x00 return -1?

As far as I know, ~0x00 equals to: First step, change 0's to 1 and 1's to zero, Second step, add 1 Step 1: 11111111111111111111111111111 (count =32)

Step 2: 11111111111111111111111111111+1=0

Where am I doing wrong?

Upvotes: 0

Views: 259

Answers (2)

John Bode
John Bode

Reputation: 123468

Step 1 is correct; there is no step 2. Unary ~ does not negate values (1 => -1), it only inverts bits (0101 => 1010).

Unary - negates values, and on a two's-complement system will do something very similar to your two steps.

Upvotes: 0

dbush
dbush

Reputation: 223972

The ~ operator does not perform 2's complement negation. You can do that with the unary - operator (assuming your machine uses 2's complement representation).

What ~ does is invert all bits. That's all it does, no adding 1.

Upvotes: 5

Related Questions