decimal to binary

Manish Jethani manish.j at gmx.net
Fri Jul 25 20:25:09 EDT 2003


manuel wrote:

>>if listByte[17] & (1 << 3):
> 
> 
> 
> Thanks!
> 
> But I don't understand completely the meaning of
> & (1 << 3)

(1 << 3) will left-shift 1 by 3 bits

00000000 00000000 00000000 00000001

giving you 8

00000000 00000000 00000000 00001000

Then you AND your number (25, for example) with 8

00000000 00000000 00000000 00011001
00000000 00000000 00000000 00001000
-----------------------------------
00000000 00000000 00000000 00001000

if you get a non-zero value, then the bit is set.

If your number is 23

00000000 00000000 00000000 00010111
00000000 00000000 00000000 00001000
-----------------------------------
00000000 00000000 00000000 00000000

then the result of the AND is 0, and that means the bit is not set.

> Can you suggest me a page in python on line manual,
> or a tutorial?

See the section on bit-wise operations in the Python Reference
Manual.

-Manish

-- 
Manish Jethani (manish.j at gmx.net)
phone (work) +91-80-51073488





More information about the Python-list mailing list