image

the sleepy snake

index :: num :: BitArray

class BitArray?

wrapper class for bit handling . The class handles up to 32 bits, bits exceeding this limit are ignored


The BitArray class defines the following attributes:

valueinteger value of the bit array

The BitArray class defines the following methods:

__init__(initval=0) constructor
__int__() returns the array as integer
__invert__() toggles all bits in the array
__long__() returns the array as long integer
__lshift__(n)shifts the array n bits to the left
__nonzero__() checks if the array is empty
__rshift__(n) shifts the array n bits to the right
__and__(integer)bitwise and
__or__(integer) bitwise or
__xor__(integer) bitwise xor
__repr__() returns the array as string representing the bits of the array
__getitem__(n) returns the n'th bit of the array
__setitem__(n, flag) adjusts the n'th bit of the array
__len__() returns the total number of bits in the array
clear() clears all bits in the array
count() returns the number of bits set in the array

sample code

arr = BitArray()
arr[3] = 1                      # set third bit
>> '100'
arr[1] = 2                      # toggle 2nd bit
>> '101'
arr[:] = 1                      # set all bits
>> '11111111111111111111111111111111'
arr.clear()                     # clear all bits
>> '0'

MY_FLAG_1 = 0
MY_FLAG_32 = 31

arr = BitArray()
arr[MY_FLAG_1] = 1
print arr[MY_FLAG_1]
>> 1