 # ~ Operator

Hi,
Can anyone please tell me how does this ~ operator works? I am aware of other bitwise operators but I am unable to understand this NOT operator. C++ is showing ~1= -2 , ~2=-3 etc… How is it happening? Thanks… @guptakeshav : Numbers or integers are represented as 2’s complement notation binary numbers .

Brief Explanation :
first bit is sign bit . ‘0’ for positive numbers and ‘1’ for negative numbers .
So positive numbers are : 000…000 (for 0) , 000…001 (for 1) , 000…0010 (for 2) and so on .
Since zero is already represented in positive numbers , no negative zero is defined .
So negative numbers are : 11…111 ( for -1 ) , 11…110 ( for -2 ) , 11…1101 ( for -3 )
eg 1. : So 1 is represented as 00…1 in binary , when you take ~ of this it becomes 1111…10 in binary which is representation of -2 .
eg 2. : So 2 is represented as 00…10 in binary and when you take ~ of this it becomes 1111101 in binary which is representation of -3 .
If you have any doubts why positive numbers follow a certain fashion or negative numbers follow a certain fashion in 2’s complement notation , you should read the wikipedia entry which will also tell you how to obtain a 2’s complement notation of a given +ve/-ve number / integer .
Hope that helps .

It’s well described here http://en.wikipedia.org/wiki/Bitwise_operation#NOT

NOT x = −x − 1

//