Here is a more optimized implementation of BitArray 'phatfingers'
class BitArray { private static final int MASK = 63; private final long len; private long bits[] = null; public BitArray(long size) { if ((((size-1)>>6) + 1) > 2147483647) { throw new IllegalArgumentException( "Field size to large, max size = 137438953408"); }else if (size < 1) { throw new IllegalArgumentException( "Field size to small, min size = 1"); } len = size; bits = new long[(int) (((size-1)>>6) + 1)]; } public boolean getBit(long pos) { return (bits[(int)(pos>>6)] & (1L << (pos&MASK))) != 0; } public void setBit(long pos, boolean b) { if (getBit(pos) != b) { bits[(int)(pos>>6)] ^= (1L << (pos&MASK)); } } public long getLength() { return len; } }
Since we use 64 fields, we increase the maximum size to 137438953408 bits, which roughly corresponds to 16 GB of memory. In addition, we use masks and bit shifts instead of division and modulo operations, reducing computation time. The improvement is quite substantial.
source share