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.

Ashu said:   8 years ago
What should be done when 1100|0011?
How the String is done?

Buck said:   8 years ago
Very well explained @ Rashmi and @Suresh.

Ranjitha said:   8 years ago
Thanks for this.

Divyadharshini said:   8 years ago
Thanks a lot @John.

Sunny deol said:   9 years ago
Agree @Suresh.

I think 15 is the correct answer.

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

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

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

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.

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.


Post your comments here:

Your comments will be displayed after verification.