+ 6

Why it outputs 7?

Print(5|3) Output: 7

25th Aug 2019, 1:40 PM
Sujithra
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
25th Aug 2019, 1:45 PM
Théophile
Théophile - avatar
+ 6
Thnq Shiny Star and Théophile ..😇😇
25th Aug 2019, 1:45 PM
Sujithra
+ 5
the symbol | is bitwise,so it will compare between 101 or 011 which gives 111 in base 2=7
25th Aug 2019, 1:44 PM
Hoh Shen Yien
Hoh Shen Yien - avatar
25th Aug 2019, 4:03 PM
Sujithra
+ 4
I am not getting this...
25th Aug 2019, 2:11 PM
Helena
Helena - avatar
+ 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)
25th Aug 2019, 5:45 PM
Brave Tea
Brave Tea - avatar
25th Aug 2019, 1:48 PM
Sanjay Kumar
Sanjay Kumar - avatar
+ 3
Thank you Shiny Star ✨ , ✨ Sujithra💫 , Théophile , Brave Tea andSanjay Kumar ! Now I understand this😀
25th Aug 2019, 9:36 PM
Helena
Helena - avatar
+ 2
Helena explain, please. You aren't getting seven as output?
25th Aug 2019, 2:32 PM
Théophile
Théophile - avatar