I want to see how bitwise NOT works through a simple example:
int x = 4;
int y;
int z;
y = ~(x<<1);
z =~(0x01<<1);
cout<<"y = "<<y<<endl;
cout<<"z = "<<z<<endl;
This results in y = -9 and z = -3. I don't see how this happen. Anyone can educate me a bit?
(x<<1)will shift the bits one, sowill become:
Which is the representation of
8. Then~will invert all the bits such that it becomes:Which is the representation of
-9.0x01isin binary, so when shifted once becomes:
And then when
~is applied we get:Which is
-3in binary