On Mar 1, 4:58 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On Mar 1, 8:53 am, "Bart Ogryczak" <[EMAIL PROTECTED]> wrote: > > > > > On Mar 1, 7:52 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> > > wrote: > > > > It seems like this would be easy but I'm drawing a blank. > > > > What I want to do is be able to open any file in binary mode, and read > > > in one byte (8 bits) at a time and then count the number of 1 bits in > > > that byte. > > > > I got as far as this but it is giving me strings and I'm not sure how > > > to accurately get to the byte/bit level. > > > > f1=file('somefile','rb') > > > while 1: > > > abyte=f1.read(1) > > > import struct > > buf = open('somefile','rb').read() > > count1 = lambda x: (x&1)+(x&2>0)+(x&4>0)+(x&8>0)+(x&16>0)+(x&32>0)+ > > (x&64>0)+(x&128>0) > > byteOnes = map(count1,struct.unpack('B'*len(buf),buf)) > > > byteOnes[n] is number is number of ones in byte n. > > This solution looks nice, but how does it work? I'm guessing > struct.unpack will provide me with 8 bit bytes
unpack with 'B' format gives you int value equivalent to unsigned char (1 byte). > (will this work on any system?) Any system with 8-bit bytes, which would mean any system made after 1965. I'm not aware of any Python implementation for UNIVAC, so I wouldn't worry ;-) > How does count1 work exactly? 1,2,4,8,16,32,64,128 in binary are 1,10,100,1000,10000,100000,1000000,10000000 x&1 == 1 if x has first bit set to 1 x&2 == 2, so (x&2>0) == True if x has second bit set to 1 ... and so on. In the context of int, True is interpreted as 1, False as 0. -- http://mail.python.org/mailman/listinfo/python-list