Why and how does System.out.println(~4); gives output -5 and System.out.println(~0); gives output -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 it's bits. But on putting -(negative) sign on number logically it becomes 1+(~x)
.
Since -x = 1+(~x)
thus (~x) = -x -1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With