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 );
}
}
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 3 of 4.
John said:
1 decade ago
8421
1011
1001
====
1001=9
0011
====
1010=10
1100
====
1110=14
1011
1001
====
1001=9
0011
====
1010=10
1100
====
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)
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)
Mahi said:
1 decade ago
11 == 1011
9 == 1001 & (AND)
----------
9 == 1001
3 == 0011 ^ (XOR)
----------
10 == 1010
12 == 1100 | (OR)
----------
14 == 1110
9 == 1001 & (AND)
----------
9 == 1001
3 == 0011 ^ (XOR)
----------
10 == 1010
12 == 1100 | (OR)
----------
14 == 1110
Joginder sharma said:
1 decade ago
Thanks Sudarshan and Satish.
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.
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.
JaiShriRam said:
1 decade ago
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.
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.
Mansi jain said:
1 decade ago
What the function ^ is does ?
Sudarshan said:
1 decade ago
1001 = (x=9)
0011 = 3
1010 = (y=9^3)
1100 = 12
1110 = y|12
0011 = 3
1010 = (y=9^3)
1100 = 12
1110 = y|12
Santhosh jaina said:
1 decade ago
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.
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
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
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers