LOGIN

NEW USER

Reply

New Forum

The ~ operator is a flip-the-bits operator. It will change all 1s to 0s and vice versa.
see this following code:
class Bitwise {
public static void main(String [] args) {
int x = 5;
System.out.println("x is initially " + x);
x = ~x;
System.out.println("~x is equal to " + x);
}
}
This program is changing every bit into its complement; thus, the output from
this program is the following:
%java Bitwise
x is initially 5
~x is equal to -6
In bit representation, the conversion looks like this,
~0000 0000 0000 0000 0000 0000 0000 0101
and converts to
1111 1111 1111 1111 1111 1111 1111 1010