[Tutor] Bitwise operation

Steven D'Aprano steve at pearwood.info
Mon Feb 28 15:44:38 CET 2011


tee chwee liong wrote:

> my IDLE seems to be giving me some funny error. i'm using Python 2.5 and Win XP. pls advise. 
>  
>>>> a=0b110010
> SyntaxError: invalid syntax


Base two literals were only added to Python in version 2.6.

[steve at sylar ~]$ python2.6
...
 >>> 0b111
7


In 2.5 and older, this is a syntax error:

[steve at sylar ~]$ python2.5
...
 >>> 0b111
   File "<stdin>", line 1
     0b111
         ^
SyntaxError: invalid syntax



Either upgrade to 2.6, or learn to convert between binary and decimal or 
hexadecimal in your head:

 >>> 0x07  # like binary 111
7


You can still convert from binary number *strings* in Python 2.5:

 >>> int("111", 2)
7






-- 
Steven


More information about the Tutor mailing list