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 ?

Asked By: Tarek

||

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
Answered By: chepner
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.