How to locate the bit in bits string?
python.list at tim.thechases.com
Tue Apr 28 17:03:21 CEST 2009
Li Wang wrote:
> If I use an integer to represent bits:
> e.g. 99 represents '1100011'
> How can I locate, say the second bit of 99(i.e. '1')?
> Although bin(99) could be used to locate it, this transform cost
> too much memory (99 only needs 2Bytes, while string '1100011' needs
> Anyone knows how to locate the second bit without using bin() function?
def get_bit(number, bit):
return (number >> bit) & 1
More information about the Python-list