Sail E0 Webinar
Question


What is the output of this program?


class bitwise_operator {
public static void main(String args[])
{
int var1 = 42;
int var2 = ~var1;
System.out.print(var1 + " " + var2);
}
}
Options:
A .  42 42
B .  43 43
C .  42 -43
D .  42 43
Answer: Option C

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:
$ javac bitwise_operator.java
$ java bitwise_operator
42 -43



Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers