Why and how System.out.println (~ 4); gives output -5 as well as System.out.println (~ 0); gives an exit of -1?
4 is 00000000 00000000 00000000 00000100 ~4 is 11111111 11111111 11111111 11111011 = -5 0 is 00000000 00000000 00000000 00000000 ~0 is 11111111 11111111 11111111 11111111 = -1
negation of a number reverses its bits. But with a positive value (negative) on the number, it logically becomes 1+(~x).
1+(~x)
Since -x = 1+(~x) in this way(~x) = -x -1
-x = 1+(~x)
(~x) = -x -1
,
11111111 11111111 11111111 11111011 = -5
: