Why is it printing 12 in both case ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is it printing 12 in both case ?

X = 12 Y = 15 Print (X&Y) Print(Y&X)

27th Jul 2019, 12:52 PM
Abhi
Abhi - avatar
4 Answers
+ 7
& is an operator for working with Boolean numbers. 12 is 1100 15 is 1111
27th Jul 2019, 1:02 PM
Mikhail Gorchanyuk
Mikhail Gorchanyuk - avatar
+ 3
Just like addition and multiplication, binary AND is commutative, i.e. changing the order of the operands do not change the result of the operation. https://en.m.wikipedia.org/wiki/Commutative_property
27th Jul 2019, 1:04 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
12: 1100 15: 1111 Applying the & operator returns 1 for each pair of 1s and 0 for literally anything. Therefore, (1&1), (1&1), (0&1), (0&1) returns 1100, or 12.
27th Jul 2019, 1:13 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
0
Thank you very much for explaining sir and thanks for thanks for that link too .
27th Jul 2019, 1:14 PM
Abhi
Abhi - avatar