What will be the output of the following Java program? [code] class bitwise_operator { public static void main(String

KellyPoima

New member
What will be the output of the following Java program?
Code:
    class bitwise_operator 
    {
        public static void main(String args[])
        {
            int var1 = 42;
            int var2 = ~var1;
            System.out.print(var1 + " " + var2);     	
        } 
    }
A. 42 42
B. 43 43
C. 42 -43
D. 42 43
 
Answer: c
Explanation: Unary not operator, ~, inverts all of the bits of its operand. 42 in binary is 00101010 in using ~ operator on var1 and assigning it to var2 we get inverted value of 42 i:e 11010101 which is -43 in decimal.
output:
Code:
$ javac bitwise_operator.java
$ java bitwise_operator
42 -43
 

Tìm kiếm

Back
Top