struct->bit access

keirr keir at mooncode.com
Tue Sep 14 05:26:27 EDT 2004


Patrick Maupin wrote:
> > def extract_bit_range_value(data, low_bit, high_bit=None):
> > if high_bit == None:
> > high_bit = low_bit
> >
> > high_bit += 1
> > return ((int(data) & ( ~(-1L << (high_bit - low_bit) ) << low_bit
> > )) >> low_bit)
>
> I ususally do something similar, but a bit simpler:
>
> def extract_bit_range_value(data, low_bit, numbits = 1):
>     return (data >> low_bit) & ((1 << numbits)-1)
>
> Regards,
> Pat

Pat,
Yep, that's simpler (and thus better)- thanks :-)

Cheers,

 Keir.




More information about the Python-list mailing list