Re: [Tutor] Bits

2006-04-04 Thread Alan Gauld

"Øyvind" <[EMAIL PROTECTED]> wrote in message
> Is it possible to read the bits (the 0's and 1's) of a string or a file
> with Python? What module would I use?

Yes, you can read the bits by reading bytes and applying
bitmasks and bitwise operations.

You can get the bytes using the struct module (explained in
the File Handling topic of my tutor) and using bitmasks
with bitwise operators to extract bits is covered in the
Operating System topic of same.

-- 
Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bits

2006-04-04 Thread Kent Johnson
Øyvind wrote:
> Hello.
> 
> Is it possible to read the bits (the 0's and 1's) of a string or a file
> with Python? What module would I use?

I don't know exactly what you mean by "read the bits" but you can use
data = open('afile', 'b').read() to get the data into a string
byte1=ord(data[1]) to get a character as binary data
byte1 & 1 to extract a single bit

You might also be interested in the struct module which lets you extract 
larger data (e.g. ints and floats) from a string.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor