Question

I'm writing a program using bitarray for example:

bytePerInt = sys.getsizeof(1)
class BitMap(object):

    def __init__(self,bits):
        self.bitsPerInt = 8*bytePerInt
        size = bits/self.bitsPerInt+1
        self.bitarray = [0]*size

    #set the bit of pos as 1
    def setBit(self,pos):
        index = pos/self.bitsPerInt
        shift = pos%self.bitsPerInt
        operator = self.bitarray[index]
        mask = 1<<shift
        operator|=mask
        self.bitarray[index] = operator

I want to get the modulus with adding instead of %, such as num&31 instead of num%32. However, bytePerInt is 24 in my computer, bitsPerInt is 24*8=192, which is not a power-of-2-number, as a result, I can't anding 191 to get the modulus, so what I can do?

Was it helpful?

Solution

Like others I'm not sure what you mean by and the essential element in the array is Int, but if you are creating a bit array of booleans (1 and 0), use bitarray.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top