Why is it wrong even if i am correct?
Question:
print(8&6)
result :
0
if i try to calculate by myself
print(bin(8))
print(bin(6))
0b1000
0b110
result after calculation :
0b100
print(0b100)
result :
4
Why is this wrong even the calculation was correct using the python bitwise operator ?
Answers:
You aren’t correct.
You are making the same mistake as if you were trying to add 10 and 1 like so:
10
+ 1
----
20
instead of
10
+ 1
---
11
You need to line up the right-most bits, padding with 0s on the left where necessary.
8 == 1000 == 1000
6 == 110 == 0110
----
0000 == 0
print(8&6)
result :
0
if i try to calculate by myself
print(bin(8))
print(bin(6))
0b1000
0b110
result after calculation :
0b100
print(0b100)
result :
4
Why is this wrong even the calculation was correct using the python bitwise operator ?
You aren’t correct.
You are making the same mistake as if you were trying to add 10 and 1 like so:
10
+ 1
----
20
instead of
10
+ 1
---
11
You need to line up the right-most bits, padding with 0s on the left where necessary.
8 == 1000 == 1000
6 == 110 == 0110
----
0000 == 0