Bitwise manipulation on python

Tim Roberts timr at probo.com
Thu Dec 20 01:03:14 EST 2001


smilenic at cheerful.com (nicole) wrote:
>
>I am a newbie to python.
>I was trying to do a xor bitwise on python using ^.
>However, it seem to show the correct thing when I do 101 ^ 011 = 110
>but when i do 1101 ^ 1111, it returns 26.
>Can anyone explain why this is happening?
>Am i using the function correctly?
>I am trying to xor two binary numbers.

That may be what you are TRYING to do, but what you're ACTUALLY doing is
xoring two DECIMAL numbers.  Integer constants in Python, as with virtually
every language in the world, are decimal.  1,101 decimal xored with 1,111
decimal equals 26 decimal.

The fact that 101 ^ 011 = 110 happens to be true in decimal as well as
binary is just a cruel joke that the universe is playing on you.

Next, try reducing the fraction 16 / 64 by cancelling out the 6s.
--
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list