Originally posted by Gong James:
What is the output of the following code?
1: int i = 45678;
2: int j = ~i;
3:
4: System.out.println(j);
A) Compilation error at line 2. ~ operator applicable to boolean values only.
B) Prints 45677.
C) Prints -45677.
D) Prints -45679.
could somebody illustrate me the execute of the code above.I have no idea with the given option .
i = 45678 which in binary is 00000000 00000000 10110010 01101110
~i = 11111111 11111111 01001101 10010001 which in decimal is -45679. Compare these two -
<pre>
i = 00000000 00000000 10110010 01101110
~i = 11111111 11111111 01001101 10010001
</pre>
Run this code, and you'll understand why. (~ changes all binary 0's to 1's and 1's to 0's)
HTH,
- Manish
[This message has been edited by Manish Hatwalne (edited October 25, 2001).]