+ 6
Why it outputs 7?
Print(5|3) Output: 7
9 ответов
+ 10
| is the binary or operator.
5 = 0101
3 = 0011
Truth table OR :
0 0 > 0
0 1 > 1
1 0 > 1
1 1 > 1
5 or 3 = 0101 | 0011
= 0111
= 7
+ 6
Thnq Shiny Star and Théophile ..😇😇
+ 5
the symbol | is bitwise,so it will compare between 101 or 011 which gives 111 in base 2=7
+ 5
Helena check thz once
https://code.sololearn.com/c5CPO7aF0PZo/?ref=app
+ 4
I am not getting this...
+ 4
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)
+ 3
Thank you Shiny Star ✨ , ✨ Sujithra💫 , Théophile , Brave Tea andSanjay Kumar ! Now I understand this😀
+ 2
Helena explain, please. You aren't getting seven as output?