I have a doubt in the following code:
public class test {
public static void main(String args[]) {
byte x = 3;
x = (byte)~x;
System.out.println(x);
}
}
The output is: -4
I have a doubt in the following code:
public class test {
public static void main(String args[]) {
byte x = 3;
x = (byte)~x;
System.out.println(x);
}
}
The output is: -4
Binary negation of numver
is the same as taking negative of number+1
. Here's why:
Binary 3 is 0011
(leading bits skipped to make it more readable).
Binary negation ~3
is 1100
.
Now, binary 4 is 0100
To determine -4, firstly write binary negation 1011
.
and then add one (see wiki) 1100