+ 3
[SOLVED] Can someone explain me this? & and |
a = 5 & 3 b = 15 & 5 c = -21 & -7 print(f" a = {a}") print(f" b = {b}") print(f" c = {c}") Output: >>> a = 1 >>> b = 5 >>> c = -23 - - - - - - - - - - - - - - - - - - - - - x = 5 | 5 y = 15 | 5 z = -21 | -7 print(f" x = {x}") print(f" y = {y}") print(f" z = {z}") Output: >>> x = 5 >>> y = 15 >>> z = -5 I expected these equation to output 1 or 0 but instead they outputs different values. Just saw this in SoloLearn while playing challenges and tried to experiment a little bit but I cant get it. Thank you!! - - - - - - - - - - - - - - - - - - - Code: https://code.sololearn.com/cA8A13A1563A
7 Réponses
+ 8
They are called Bitwise operators.
Check it out for reference
https://www.sololearn.com/learn/4070/?ref=app
+ 5
https://www.sololearn.com/learn/4072/?ref=app
https://www.sololearn.com/learn/4073/?ref=app
15 => 0000 1111
5 => 0000 0101 &
--------------------
5 => 0000 0101
15 => 0000 1111
5 => 0000 0101 |
--------------------
15 => 0000 1111
+ 5
+ 3
《 Nicko12 》 I think you don't need to remember the all numbers.. just remember this...
decimal 128 64 32 16 8 4 2 1
now let's say you want to convert decimal 5 to binary
then find the values which sum is 5
Ex. 4+1 = 5
we choose 4 and 1 right? so write 4 and 1 as 1 and others 0
00000101
128 0
64 0
32 0
16 0
8 0
4 1
2 0
1 1
4+1 =5 that's why we choose 4 and 1 values as 1 and others are 0
5 = 00000101
Example 2 :
decimal 34
find the numbers from above given number whose sum is 34
32+2 = 34
so 32 and 2 write as 1 and others are 0
so the binary of 34 is 00100010
also we can strip the leading zeros and write it as 100010
just remember this sequence and all done!
128 64 32 16 8 4 2 1
Hope you got it bro!
if you have any doubts feel free to ask 😊
+ 2
But how do we know their value in binary? Do we need to memorize or familiarize with it or is there such pattern to it? in order to solve it mentally like with challenges.