0
Need help understanding | operator
I am having trouble understanding how we get the answer 30 here. I understand the | symbol is interpreted as a bitwise OR, but that doesn’t in any way help me understand why the answer in 30 lol. Any help would be appreciated! Input: def f(m, n): return m|n print(f(10, 20)) output: 30
6 Respuestas
+ 1
The | operator corresponds to the logical OR expression. This means that your arguments will be treated as binary numbers, and starting with the lowest value digits (rightmost) for each one number, if they are both 0 the result's digit at this position will also be 0, otherwise it will be 1.
For example 10(decimal) = 01010(binary) and 20(decimal) = 10100(binary). So, 01010 OR 10100 = 11110(binary) = 30(decimal).
+ 2
It turns on every bit. Basically it combines any place that has a 1.
See these;
https://www.tutorialspoint.com/JUMP_LINK__&&__python__&&__JUMP_LINK/bitwise_operators_example.htm
https://wiki.python.org/moin/BitwiseOperators
+ 2
In this case you have to write the binary representation of the two numbers,then they will be checked with the bitwise or operator
01010 for 10
10100 for 20
01010 | 10100 ===> 11110 convert it to decimal gives us 30
Don't forget that 0|1 == 1 and 1|1 == 1
+ 1
thank you so much!
+ 1
In binary, 10 is 1010, 20 is 10100. 10|20 = 11110 which is 30. This case gives you by chance the sum, but it is not really the case 😁
+ 1
wow thank all you guys. This is super helpful