// little-endian sclass BitBuffer is IntSize { new ByteBuffer byteBuffer; // TODO (optimization): use a growing circular buffer int currentByte, currentBit; void add(bool b) { if (b) currentByte |= 1 << currentBit; if (currentBit == 7) { byteBuffer.add((byte) currentByte); currentByte = 0; currentBit = 0; } else ++currentBit; } bool get(int iBit) { ret (byteBuffer.get(iBit >> 3) & (1 << (iBit & 7))) != 0; } bool hasFullByte() { ret !byteBuffer.isEmpty(); } byte popFullByte() { ret byteBuffer.popFirst(); } public int size() { ret byteBuffer.size()*8 + currentBit; } // TODO: optimize void writeBits(int data, int nBits) }