Java Programming - Operators and Assignments - Discussion

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 );
    }
}

[A]. 0
[B]. 7
[C]. 8
[D]. 14

Answer: Option D

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.


S. Jasmine said: (Jul 31, 2010)  
I can't understand that how this answer came, can somebody explain me?

Vineeth said: (Nov 26, 2010)  
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: (Dec 10, 2010)  
Well anyone make more clear.

Sumit said: (Dec 24, 2010)  
Please explain this clearly and complete. I can't understand what is happning.

Suresh said: (Mar 26, 2011)  
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: (May 28, 2011)  
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: (Jun 19, 2011)  
1001 = (x=9)
0011 = 3
1010 = (y=9^3)
1100 = 12
1110 = y|12

Mansi Jain said: (Oct 7, 2011)  
What the function ^ is does ?

Jaishriram said: (Dec 19, 2011)  
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: (Feb 1, 2012)  
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.

Joginder Sharma said: (Jul 8, 2012)  
Thanks Sudarshan and Satish.

Mahi said: (Dec 20, 2012)  
11 == 1011
9 == 1001 & (AND)
----------
9 == 1001
3 == 0011 ^ (XOR)
----------
10 == 1010
12 == 1100 | (OR)
----------
14 == 1110

Jamie said: (Apr 17, 2013)  
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: (Jan 24, 2015)  
8421
1011
1001
====
1001=9
0011
====
1010=10
1100
====
1110=14

Piyali said: (Jul 10, 2016)  
Thanks @Satish.

Rashmi said: (Aug 7, 2016)  
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: (Sep 2, 2016)  
Thanks @Rashmi.

Jitesh Powankar said: (Nov 5, 2016)  
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: (Nov 29, 2016)  
Thanks a lot, @Rashmi.

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

Sachin J said: (Dec 17, 2016)  
Thank you @Jaishriram.

Sunny Deol said: (Jan 21, 2017)  
Agree @Suresh.

I think 15 is the correct answer.

Divyadharshini said: (May 16, 2017)  
Thanks a lot @John.

Ranjitha said: (Oct 26, 2017)  
Thanks for this.

Buck said: (Oct 31, 2017)  
Very well explained @ Rashmi and @Suresh.

Ashu said: (Dec 6, 2017)  
What should be done when 1100|0011?
How the String is done?

Raghavendar said: (Feb 20, 2018)  
int x=9&11;
& ---->And operation means both are true then output is true otherwise false
so 9----->1001(binary form)
11---->1011
----------------------------------------
1001 so it's----->9
------------------------------------------------

y=x^3
^ ------->XOR operation so if both inputs are same then output is false otherwise false
So, x value is 9 binary is----1001.

y=1001^0011=1010 (decimal is 10)
then;
y|12
-----------> |-- OR operation means if both inputs are false then the output is false otherwise true.

y value----->1010
12 binary-> 1100
----------------------------------
1110 ------->14.

So the answer is 14.

Prudhvi said: (Feb 28, 2018)  
Well explained, thanks @Jitesh Powankar.

Dheeraj Reddy said: (Sep 17, 2018)  
11= 1101.
9 = 1001.
9 = 1001.
3 = 0011.
10 = 1010.
12 = 1100.
14 = 1110.

Manoj said: (Oct 5, 2018)  
XOR means, the output will be 1 if both the input are exclusive i.e. both the input should be different for example (1, 0) (0, 1) so this gives output 1. Hence output of (1, 1) (0, 0) is 0.

Sneha Sonawane said: (Dec 15, 2018)  
x=11&9 ie 1011(11) & 1001 (9) =1001 (9) //Binary conversions therefore now x=9;

y=x^3 ie 9^3 (^ is Ex OR operation) 1001^0011= 1010 (10) // Binary conversions therefore now y=10;.

y|12 ie 10|12 ( | is OR operator) 1010 | 1100 =1110 (14) ///Binary conversions therefore now y=14;

Keerthana said: (Jul 8, 2019)  
Why we are choosing 9 as value for X? There is an another value 11.

Atul said: (Oct 9, 2019)  
For know about operate:

For AND operate 1,1 ---result will be 1
like
(1,1) --->1
(0,0)--->0
(0,1)--->0
(1,0)--->0

For OR operate 0,0----result will be 0
like
(1,1) --->1
(1,0) --->1
(0,1) --->1
(0,0)--->0

for EXor operate 0,0----result will be 0 and 1,1 ---result will be 1 ,other than 1
(0,0) --->0
(1,0) --->1
(0,1) --->1
(1,1) --->0
--------------------------------------------------------
---------------------------------------------------------
Binary value Decimal value
1100 12
1001 9
1011 11
0011 3


1001 9
1011 11
===========
1001--------(And operate) result is 1001 which is equals to = 9 in decimal value
--------------------------------------------------------------------------

1001 9 ( decimal value)
0011 3 ( decimal value)

-->9^3
1001
0011
===========
1010 ...>(EXor)

result is 1010 which is equals to = 10 in decimal value
--------------------------------------------------------------------------
1100 12 ( decimal value)

12 | X
| -> OR operator.
1010
1100
===========
1110 (OR operator) result is 1110 which is equals to = 14 in decimal value

Iftekhar-Ul-Haque said: (Mar 13, 2020)  
11 == 1011
9 == 1001 & (AND)
----------
9 == 1001
3 == 0011 ^ (XOR)
----------
10 == 1010
12 == 1100 | (OR)
----------
14 == 1110.

Arun said: (Mar 23, 2021)  
11=1011
9= 1001(binary value)
First condition (int x = 11 & 9;)
&-And operator check both are true else return false so
X take 9.
X=9.

Second condition (int y = x ^ 3;)
^ power operator
X=1001 ^
3=0011.

Outcome is 1010
1010 is a value of 10.
Third condition( y | 12 ) is 0r operator,
(10 |12) in the binary value.
10 = 1010 ( 0r operator).
12=1100.

Output is 1110 is a value of 14.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.