On Oct 10, 10:37 pm, "Aaron \"Castironpi\" Brady" <[EMAIL PROTECTED]> wrote: > On Oct 9, 5:30 pm, "Hendrik van Rooyen" <[EMAIL PROTECTED]> wrote: > > > > > Is there a canonical way to address the bits in a structure > > like an array or string or struct? > > > Or alternatively, is there a good way to combine eight > > ints that represent bits into one of the bytes in some > > array or string or whatever? snip > > class BitSet: > def __init__( self, value ): > self.value= value > def __setitem__( self, index, value ): > if value: > self.value= self.value| (1<< index) > elif self[ index ]: > self.value= self.value^ (1<< index) > def __getitem__( self, index ): > return self.value& (1<< index ) snip
This could read: def __getitem__( self, index ): return 1 if self.value& (1<< index ) else 0 Or you could shift self.value, and mask with unity. -- http://mail.python.org/mailman/listinfo/python-list