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.

Atul said:   6 years ago
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
(2)

Raghavendar said:   8 years ago
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.

ARUN said:   4 years ago
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.
(2)

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.

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

Sneha sonawane said:   7 years ago
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;
(4)

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

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.

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)

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.