How print(5|3) work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How print(5|3) work?

Explain in detain

31st Aug 2019, 10:58 AM
Jitendra
Jitendra - avatar
2 Answers
+ 3
5 = 101 in binary 3 = 011 in binary 1 or 1 = 1 0 or 1 = 1 1 or 0 = 1 0 or 0 = 0 1 0 1 0 1 1 # or'd so 7 = 1 1 1
31st Aug 2019, 11:11 AM
rodwynnejones
rodwynnejones - avatar
+ 2
when the bitwise operator | (which is “or”) is used it takes a look at the binary of the number What it does next is check every position of both numbers for 1s if it finds a 1 in one of the position on either side of the | operator it sets that position to 1 for example 5|10 binary of 5 is 0101 (or 101) binary of 10 is 1010 the | operator will check each position in the binary code ( 0101 has 4 positions; as does 1010)(you can always add more zeroes to the front of the binary :)) if it finds a 1 in position 1 it will turn that to one position 1 in 0101 is a 0, but in 1010 it is a 1, so it turns position 1 into 1 position 2 in 0101 is a 1, so turns position 2 to 1 etc etc which gives us, at the end of the calculation: 1111 which is binary for 15 5|10 == 15 other examples when looking st binary numbers 0000|1111 == 1111 (0|15==15) 1100 | 1111 == 1111 (12|15==15)
31st Aug 2019, 12:13 PM
Brave Tea
Brave Tea - avatar