An updated version of the bitmap library from https://github.com/wanji/bitmap since the original project is no longer maintained.
This package provides a BitMap
class which is an array of bits stored in compact format.
bitmap
can be installed from pip
:
pip3 install bitmap2
BitMap(maxnum)
: construct aBitMap
object withmaxnum
bitsset(pos)
: set the bit at positionpos
to 1reset(pos)
: reset the bit at positionpos
to 0flip(pos)
: flip the bit at positionpos
count()
: return the number of 1ssize()
: return the size of theBitMap
test(pos)
: check if bit at positionpos
has been set to 1any()
: check if any bit in theBitMap
has been set to 1none()
: check if none of the bits in theBitMap
has been set to 1all()
: check if all bits in theBitMap
has been set to 1nonzero()
: return list of indexes of all non-zero bitsnonzeros()
: return generator of indexes of all non-zero bitszero()
: return list of indexes of all zero bitszeros()
: return generator of indexes of all zero bitstostring()
: convert aBitMap
object to0
and1
stringtofile(path)
: save aBitMap
object to a file onpath
fromstring(bitstring)
: create aBitMap
object from0
and1
stringfromhexstring(hexstring)
: construct aBitMap
object from hex stringfromfile(path, maxnum)
: construct aBitmap
object from a file atpath
of sizemaxnum
bits
from bitmap import BitMap
bm = BitMap(32)
print bm.tostring()
bm.set(1)
print bm.tostring()
bm = BitMap.fromstring("00011101")
print bm.tostring()
bm.flip(1)
print bm.tostring()