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 3 of 4.

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.

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

Joginder sharma said:   1 decade ago
Thanks Sudarshan and Satish.

Mahi said:   1 decade ago
11 == 1011
9 == 1001 & (AND)
----------
9 == 1001
3 == 0011 ^ (XOR)
----------
10 == 1010
12 == 1100 | (OR)
----------
14 == 1110

Jamie said:   1 decade ago
8421

1011
1001 & (Bitwise And - Set bit if both bits set)
----
1001 (9)

1001
0011 ^ (Bitwise Exclusive Or - Set bit if ONLY one bit is set)
----
1010 (10)

1010
1100 | (Bitwise Or - Set bit if either bit is set)
----
1110 (14)


Post your comments here:

Your comments will be displayed after verification.