I have been performing bitwise operation on a variable.
int p=3,q=5;
int a=~p,b=~q; //complement a and b
printf("%d %d\t%d %d",p,a,q,b);
The theoretical output for ‘b’ is 10 and in case if it’s signed, it has to be -2.
But the output is -6.
Can someone explain me the working of it?
~is the bitwise complement operator in c (or python) which essentially calculates-x - 1.So a table would look like:
In two’s complement representation, if a number x’s most significant bit is 1, then the actual value would be −(~x + 1).
For instance,
This is a natural representation of negative numbers, because
See http://en.wikipedia.org/wiki/Two%27s_complement for detail.