@Test public void testBigEndianWrite2() { byte[] x = new byte[4]; BitBuffer bitbuf = new BitBuffer(x); bitbuf.putBits(0x01020304, 32); assertEquals("01020304", StringConverter.arrayToHexString(x)); }
public void putBits(long value, int numBits) { if (numBits > 64) { throw new IllegalArgumentException("Invalid numBits " + numBits + " max value: 64"); } long v = (numBits < 64) ? value & ((1L << numBits) - 1) : value; if (byteOrder == ByteOrder.LITTLE_ENDIAN) { putBitsLE(v, numBits); return; } int bytepos = position >> 3; int n = numBits; int fbb = -position & 0x7; if (fbb > 0) { if (n <= fbb) { int m = fbb - n; position += numBits; b[idx(bytepos)] &= ~(((1 << n) - 1) << m); b[idx(bytepos)] |= (v << m); return; } else { n -= fbb; b[idx(bytepos)] &= -1 << fbb; b[idx(bytepos)] |= (v >>> n); v = v & ((1L << n) - 1); bytepos++; } } while (n > 8) { n -= 8; b[idx(bytepos)] = (byte) (v >>> n); v = v & ((1L << n) - 1); bytepos++; } b[idx(bytepos)] &= ((1 << (8 - n)) - 1); b[idx(bytepos)] |= v << (8 - n); position += numBits; }
BitBuffer { public void putBits(long value, int numBits) { if (numBits > 64) { throw new IllegalArgumentException("Invalid numBits " + numBits + " max value: 64"); } long v = (numBits < 64) ? value & ((1L << numBits) - 1) : value; if (byteOrder == ByteOrder.LITTLE_ENDIAN) { putBitsLE(v, numBits); return; } int bytepos = position >> 3; int n = numBits; int fbb = -position & 0x7; if (fbb > 0) { if (n <= fbb) { int m = fbb - n; position += numBits; b[idx(bytepos)] &= ~(((1 << n) - 1) << m); b[idx(bytepos)] |= (v << m); return; } else { n -= fbb; b[idx(bytepos)] &= -1 << fbb; b[idx(bytepos)] |= (v >>> n); v = v & ((1L << n) - 1); bytepos++; } } while (n > 8) { n -= 8; b[idx(bytepos)] = (byte) (v >>> n); v = v & ((1L << n) - 1); bytepos++; } b[idx(bytepos)] &= ((1 << (8 - n)) - 1); b[idx(bytepos)] |= v << (8 - n); position += numBits; } }
BitBuffer { public void putBits(long value, int numBits) { if (numBits > 64) { throw new IllegalArgumentException("Invalid numBits " + numBits + " max value: 64"); } long v = (numBits < 64) ? value & ((1L << numBits) - 1) : value; if (byteOrder == ByteOrder.LITTLE_ENDIAN) { putBitsLE(v, numBits); return; } int bytepos = position >> 3; int n = numBits; int fbb = -position & 0x7; if (fbb > 0) { if (n <= fbb) { int m = fbb - n; position += numBits; b[idx(bytepos)] &= ~(((1 << n) - 1) << m); b[idx(bytepos)] |= (v << m); return; } else { n -= fbb; b[idx(bytepos)] &= -1 << fbb; b[idx(bytepos)] |= (v >>> n); v = v & ((1L << n) - 1); bytepos++; } } while (n > 8) { n -= 8; b[idx(bytepos)] = (byte) (v >>> n); v = v & ((1L << n) - 1); bytepos++; } b[idx(bytepos)] &= ((1 << (8 - n)) - 1); b[idx(bytepos)] |= v << (8 - n); position += numBits; } BitBuffer(byte[] b);  BitBuffer(byte[] b, int offset); }
BitBuffer { public void putBits(long value, int numBits) { if (numBits > 64) { throw new IllegalArgumentException("Invalid numBits " + numBits + " max value: 64"); } long v = (numBits < 64) ? value & ((1L << numBits) - 1) : value; if (byteOrder == ByteOrder.LITTLE_ENDIAN) { putBitsLE(v, numBits); return; } int bytepos = position >> 3; int n = numBits; int fbb = -position & 0x7; if (fbb > 0) { if (n <= fbb) { int m = fbb - n; position += numBits; b[idx(bytepos)] &= ~(((1 << n) - 1) << m); b[idx(bytepos)] |= (v << m); return; } else { n -= fbb; b[idx(bytepos)] &= -1 << fbb; b[idx(bytepos)] |= (v >>> n); v = v & ((1L << n) - 1); bytepos++; } } while (n > 8) { n -= 8; b[idx(bytepos)] = (byte) (v >>> n); v = v & ((1L << n) - 1); bytepos++; } b[idx(bytepos)] &= ((1 << (8 - n)) - 1); b[idx(bytepos)] |= v << (8 - n); position += numBits; } BitBuffer(byte[] b);  BitBuffer(byte[] b, int offset); long getBits(int numBits); void putBits(long value, int numBits); void put(byte[] src, int offset, int length); void put(byte[] src); void putByte(byte c); int getPosition(); void setPosition(int position); void setByteOrder(ByteOrder order); ByteOrder getByteOrder(); byte getByte(); void getByteArray(byte[] dst); int sizeInBits(); int arrayLength(); BitBuffer slice(); byte[] array(); int offset(); int remainingBytes(); void skip(int numBits); }
BitBuffer { public void putBits(long value, int numBits) { if (numBits > 64) { throw new IllegalArgumentException("Invalid numBits " + numBits + " max value: 64"); } long v = (numBits < 64) ? value & ((1L << numBits) - 1) : value; if (byteOrder == ByteOrder.LITTLE_ENDIAN) { putBitsLE(v, numBits); return; } int bytepos = position >> 3; int n = numBits; int fbb = -position & 0x7; if (fbb > 0) { if (n <= fbb) { int m = fbb - n; position += numBits; b[idx(bytepos)] &= ~(((1 << n) - 1) << m); b[idx(bytepos)] |= (v << m); return; } else { n -= fbb; b[idx(bytepos)] &= -1 << fbb; b[idx(bytepos)] |= (v >>> n); v = v & ((1L << n) - 1); bytepos++; } } while (n > 8) { n -= 8; b[idx(bytepos)] = (byte) (v >>> n); v = v & ((1L << n) - 1); bytepos++; } b[idx(bytepos)] &= ((1 << (8 - n)) - 1); b[idx(bytepos)] |= v << (8 - n); position += numBits; } BitBuffer(byte[] b);  BitBuffer(byte[] b, int offset); long getBits(int numBits); void putBits(long value, int numBits); void put(byte[] src, int offset, int length); void put(byte[] src); void putByte(byte c); int getPosition(); void setPosition(int position); void setByteOrder(ByteOrder order); ByteOrder getByteOrder(); byte getByte(); void getByteArray(byte[] dst); int sizeInBits(); int arrayLength(); BitBuffer slice(); byte[] array(); int offset(); int remainingBytes(); void skip(int numBits);  }