+ 5

How does bitwise not operator(~)work ?

int a=10; System.out.println(~a); //ans is -11 //can anyone explain this??

25th Mar 2017, 12:38 PM
Doolitha Samaranayake
Doolitha Samaranayake - avatar
1 Answer
+ 11
The bitwise not operator (~) changes any 0 in the binary value of 10 to a 1, and any 1 in the binary value of 10 to a 0. So basically, it switches the 0s and 1s. To find the answer, first convert 10 to binary. Then switch every 1 out with a 0 and every 0 out with a 1. Convert this back into binary and you get -11. I hope this helps! Respond back if you are still confused.
25th Mar 2017, 1:30 PM
J.G.
J.G. - avatar