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

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

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)

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)

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)


Post your comments here:

Your comments will be displayed after verification.