Mike Aluydiavon Stack Overflow • Recommends
On “How does Python's bitwise complement operator (~ tilde) work?”
int a=4;
System.out.println(~a);
Result would be :-5
'~' of any integer in java represents 1's complement of the no.
for example i am taking ~4,which means in binary representation 0100.
first ,
length of an integer is four bytes,i.e 4*8(8 bits for 1 byte)=32.
So in system memory 4 is represented as
0000 0000 0000 0000 0000 0000 0000 0100
now ~ operator will perform 1's complement on the above binary no
i.e 1111 1111 1111 1111 1111 1111 1111 1011->1's complement
the most significant bit represents sign of the no(either - or +)
if it is 1 then sign is '-'
if it is 0 then sign is '+'
as per this our result is a negative number,
in java the negative numbers are stored in 2's complement fo…