Java binary padding operator

Why and how System.out.println (~ 4); gives output -5 as well as System.out.println (~ 0); gives an exit of -1?

+4
source share
3 answers
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
+10
source

negation of a number reverses its bits. But with a positive value (negative) on the number, it logically becomes 1+(~x).

Since -x = 1+(~x) in this way(~x) = -x -1

+1
source

,

11111111 11111111 11111111 11111011 = -5

:

0

All Articles