Java Programming - Operators and Assignments - Discussion

Discussion Forum : Operators and Assignments - Finding the output (Q.No. 9)
9.
What will be the output of the program?
class Bitwise 
{
    public static void main(String [] args) 
    {
        int x = 11 & 9;
        int y = x ^ 3;
        System.out.println( y | 12 );
    }
}
0
7
8
14
Answer: Option
Explanation:

The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9. The ^ operator produces a 1 bit when exactly one bit is 1; the result of this operation is 10. The | operator produces a 1 bit when at least one bit is 1; the result of this operation is 14.

Discussion:
34 comments Page 1 of 4.

S. Jasmine said:   2 decades ago
I can't understand that how this answer came, can somebody explain me?

Vineeth said:   1 decade ago
helo jasmin..here is the explanation
write numbers in binary
0000
0001
0010
....
....
....
1111
so we want to find out 11&9.
ie 1001
1000
----
1000----->9
using this value we can find y in successive operations as explained.
i think its a good question..thank you

Jaz said:   1 decade ago
Well anyone make more clear.

Sumit said:   1 decade ago
Please explain this clearly and complete. I can't understand what is happning.

Suresh said:   1 decade ago
11 in binary form is 1011
9 in binary form is 1001
so 11&9=1001 ie equals 9
and y=x^3 ==
x value is 9 then
y=9^3==1001(ie 9 binary value)
0011(ie 3 binary value)
so y=1011 ie equals to 11
then y|12 is equals to 11|12 == 1011
1100
==15 so the =answer is 15

Santhosh jaina said:   1 decade ago
Do smart work by looking at options we can say tht first three ans are lesser than 12 while doing OR operation the value will automatically increase.

Sudarshan said:   1 decade ago
1001 = (x=9)
0011 = 3
1010 = (y=9^3)
1100 = 12
1110 = y|12

Mansi jain said:   1 decade ago
What the function ^ is does ?

JaiShriRam said:   1 decade ago
Mansi Jain: ^ is EX-OR function,
which means if toggle answre is 1 else 0
eg: 9^3 ==
1001
0011
------
1010 == number 10
ook when the binary digit are same,
we get 0 as resut, when they toggle we get 1.

Satish said:   1 decade ago
Binary of(11)=1011
Binary of (9)=1001

Perform & ------
Result X=1001 which is in decimal(9)
Now X^3 where x=1001
Binary of(3) = 0011

Perform ex-or ------
Result Y=1010 which is in decimal(10)
Now perform (Y|12)=1010
Binary of (12) = 1100

Perform or(|) -----

Result = 1110 which is in decimal (14).

Hence the ans is 14.


Post your comments here:

Your comments will be displayed after verification.