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.

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)

Iftekhar-ul-haque said:   5 years ago
11 == 1011
9 == 1001 & (AND)
----------
9 == 1001
3 == 0011 ^ (XOR)
----------
10 == 1010
12 == 1100 | (OR)
----------
14 == 1110.
(2)

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)

Keerthana said:   6 years ago
Why we are choosing 9 as value for X? There is an another value 11.

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)

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

Dheeraj Reddy said:   7 years ago
11= 1101.
9 = 1001.
9 = 1001.
3 = 0011.
10 = 1010.
12 = 1100.
14 = 1110.
(1)

Prudhvi said:   8 years ago
Well explained, thanks @Jitesh Powankar.
(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.

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


Post your comments here:

Your comments will be displayed after verification.