Hi, I found an exercise like this:
and
>>> 5&7
5
or
>>> 5|7
7
More in none of the cases I can understand how it works, could someone please tell me.
Hi, I found an exercise like this:
and
>>> 5&7
5
or
>>> 5|7
7
More in none of the cases I can understand how it works, could someone please tell me.
In English they are called bitwise operators:
The first ( & ) is the bitwise AND : It decomposes the numbers in binary, and the “add” is done as follows:
1x1=1, 1x0=0, 0x1=0, 0x0=0
010 1 (5) AND
001 1 (3)
———————
000 1 (1)
On the other hand, for the or ( | ), the “add” is done as follows:
The result is 0 if both numbers are 0, the others are 1
0 101 OR
0 011
————————
0 111