+ public void appendBit(boolean bit) {\r
+ ensureCapacity(size + 1);\r
+ if (bit) {\r
+ bits[size >> 5] |= (1 << (size & 0x1F));\r
+ }\r
+ size++;\r
+ }\r
+\r
+ /**\r
+ * Appends the least-significant bits, from value, in order from most-significant to\r
+ * least-significant. For example, appending 6 bits from 0x000001E will append the bits\r
+ * 0, 1, 1, 1, 1, 0 in that order.\r
+ */\r
+ public void appendBits(int value, int numBits) {\r
+ if (numBits < 0 || numBits > 32) {\r
+ throw new IllegalArgumentException("Num bits must be between 0 and 32");\r
+ }\r
+ ensureCapacity(size + numBits);\r
+ for (int numBitsLeft = numBits; numBitsLeft > 0; numBitsLeft--) {\r
+ appendBit(((value >> (numBitsLeft - 1)) & 0x01) == 1);\r
+ }\r
+ }\r
+\r
+ public void appendBitArray(BitArray other) {\r
+ int otherSize = other.getSize();\r
+ ensureCapacity(size + otherSize);\r
+ for (int i = 0; i < otherSize; i++) {\r
+ appendBit(other.get(i));\r
+ }\r
+ }\r
+\r
+ public void xor(BitArray other) {\r
+ if (bits.length != other.bits.length) {\r
+ throw new IllegalArgumentException("Sizes don't match");\r
+ }\r
+ for (int i = 0; i < bits.length; i++) {\r
+ // The last byte could be incomplete (i.e. not have 8 bits in\r
+ // it) but there is no problem since 0 XOR 0 == 0.\r
+ bits[i] ^= other.bits[i];\r
+ }\r
+ }\r
+\r
+ /**\r
+ *\r
+ * @param bitOffset first bit to start writing\r
+ * @param array array to write into. Bytes are written most-significant byte first. This is the opposite\r
+ * of the internal representation, which is exposed by {@link #getBitArray()}\r
+ * @param offset position in array to start writing\r
+ * @param numBytes how many bytes to write\r
+ */\r
+ public void toBytes(int bitOffset, byte[] array, int offset, int numBytes) {\r
+ for (int i = 0; i < numBytes; i++) {\r
+ int theByte = 0;\r
+ for (int j = 0; j < 8; j++) {\r
+ if (get(bitOffset)) {\r
+ theByte |= 1 << (7 - j);\r
+ }\r
+ bitOffset++;\r
+ }\r
+ array[offset + i] = (byte) theByte;\r
+ }\r
+ }\r
+\r