Efficient Bit addressing in Python.

Tino Wildenhain tino at wildenhain.de
Fri Oct 10 05:33:31 EDT 2008


Hendrik van Rooyen wrote:
> "Tino Wildenhain"  wrote:
> 
> 
>> byte1 byte2? this does not look very practical
>> to me. In the simplest form of storing
>> your values in a text string, you could just
>> use ord() to get the byte value and
>> operate on it with 1<<0 1<<1 1<<3 and so on.
>>
>> If you want, put a module in which defines the
>> constants
>>
>> bit1=1<<0
>> bit2=1<<1
>>
>> and so on and use it via
>> if byte & bit1: ...
> 
> This is what I meant by "jumping through hoops".
> 
>> more efficiently for operations on really big
>> bit strings is probably just using integers.
> 
> Sure, one could for instance make a list of eight-entry lists:
> 
> io = [[b0,b1,b2,b3,b4,b5,b6,b7],    ]

what should that represent? Which byte order
do you have in mind etc?
> 
> Then the hoop jumping goes in the opposite
> direction - to get hold of an actual byte, you
> have to rotate the bits into some byte one at a
> time.

Well, thats one would expect by your proposed interface.

Can you perhaps outline what kind of application
you have in mind and which operations look meaningfull
to that?

I'm familar with embedded hardware where you would have
a couple of registers where you usually only have
the distinction between 8-bit or 16 bit flag registers
where it makes sense to individually influence bits.
Personally I can't follow you on the path to have
arbitrary lengthy bitfields - even nore to
have artifically attributes (like bit5) on them.

Just using a big integer to represent your bitfield
and using binary operators on it does not sound
so wrong to me in this case.

Of course one could create a type which derives
from sequence types and implement something
like

bitfieldinstance[bitindex] (where 0 is LSB)

would you like fries... err slices with that?

e.g.

 >>> bf=BitField(10)
 >>> int(bf[1:])
5

?



> This approach has the advantage that you can
> add a ninth "dirty" bit to indicate that the "byte" 
> in question needs to be written out.

What do you mean by "written out" to where?

> Is there not some OO way of hiding this
> bit banging complexity?

foo & bar is complex? So you want to replace foo + bar
as well with something? ;)

> Using getters and setters? - I tend to go "tilt" 
> like a cheap slot machine when I read that stuff.

Getters setters? Where would that improve the situation
beside having to write lots of unneccessary code?

Regards
Tino


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3241 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20081010/6496669f/attachment.bin>


More information about the Python-list mailing list