struct->bit access

Patrick Maupin pmaupin at speakeasy.net
Tue Sep 14 06:51:25 CEST 2004


> 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



More information about the Python-list mailing list