I just came across this question on a mock exam:
What will be the result if you attempt to compile and run the following code ?
public class Flip{
public static void main(
String argv[]){
System.out.println(~4);
}
}
The answer is -5.
I understand that the binary representation of 4 is:
0000 0000 0000 0000 0000 0000 0000 0100
and that after the flip of all the bits, the binary result is:
1111 1111 1111 1111 1111 1111 1111 1011
I know that a 1 in the leftmost position means the number is negative, but I don't understand how th above represents -5.
I realize this is kind of a basic question, but can anyone explain? Thanks.