Bitwise operations in Python?

Paul Rubin http
Thu Aug 18 19:31:03 CEST 2005


Carl <phleum_nospam at chello.se> writes:
> IBITS(I, POS, LEN)
> Extracts a sequence of bits.
> The result has the value of the sequence of LEN bits in I beginning at bit
> POS, right-adjusted and with all other bits zero.
> 
> The bits are numbered from 0 to BIT_SIZE(I)-1, from right to left.
> 
> Examples
> 
> IBITS (14, 1, 3) has the value 7. 

>>> def ibits(i,pos,len):
        return (i >> pos) & ~(-1 << len)

>>> ibits(14,1,3)
7



More information about the Python-list mailing list