Why is: public class MyClass { public static void main(String[] args ) { int x = 5; x &= 3; System.out.println(x); } } = 1?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is: public class MyClass { public static void main(String[] args ) { int x = 5; x &= 3; System.out.println(x); } } = 1??

Why is the answer equal to one? Please help

21st Oct 2019, 11:33 AM
Jibrilla Abdul-Razak
Jibrilla Abdul-Razak - avatar
3 Answers
+ 8
& is a bitwise AND operator, so you need to evaluate this expression in binary. (assume size of int type is 1 byte to see it clearly). int x=5; // 5 --> 0000 0101 // 3 --> 0000 0011 x&=3; // 5&3 --> 0000 0001 --> 1 in decimal system 1 -> true & 0 -> false then 1&1 will result in true and rest all combinations will result in false as an expression will be true only if all conditions in it are true.
21st Oct 2019, 1:12 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
Thank you Mr. Gaurav
22nd Oct 2019, 5:10 PM
Jibrilla Abdul-Razak
Jibrilla Abdul-Razak - avatar
+ 1
Got it now, thanks
22nd Oct 2019, 5:11 PM
Jibrilla Abdul-Razak
Jibrilla Abdul-Razak - avatar