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

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)

John said:   1 decade ago
8421
1011
1001
====
1001=9
0011
====
1010=10
1100
====
1110=14

Piyali said:   9 years ago
Thanks @Satish.

Rashmi said:   9 years 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)

Ekta said:   9 years ago
Thanks @Rashmi.

Jitesh Powankar said:   9 years ago
Step 1) 11 & 9.
11 -> 1011.
9 -> 1001.
& -> work like and operator.

1011
1001
----------
1001.

Step 2) X ^ 3.
^ -> XOR operator.
9 -> 1001
3 -> 0011
-------------
1010.

Step 3) 12 | X
x -> OR operator.
1100
1010
---------
1110
1110 -> 14.

Accel said:   9 years ago
Thanks a lot, @Rashmi.

I didn't understand why 1 or 1 = 0, but you covered that up!

Sachin J said:   9 years ago
Thank you @Jaishriram.


Post your comments here:

Your comments will be displayed after verification.