Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is it wrong even if i am correct?

print(8&6)

result :

0

if i try to calculate by myself

print(bin(8))
print(bin(6))

0b1000
0b110

result after calculation :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

0b100


print(0b100)

result :

4

Why is this wrong even the calculation was correct using the python bitwise operator ?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading