@Test(dataProvider = "shiftBits") public void shiftRight(final Integer shift, final Integer bytes) { final long seed = System.currentTimeMillis(); final Random random = new Random(seed); final byte[] data = new byte[bytes]; for (int i = 0; i < data.length*8; ++i) { Bits.set(data, i, random.nextBoolean()); } Bits.shiftRight(data, shift); random.setSeed(seed); for (int i = 0; i < shift; ++i) { random.nextBoolean(); Assert.assertEquals(Bits.get(data, data.length*8 - 1 - i), false); } for (int i = 0, n = data.length*8 - shift; i < n; ++i) { Assert.assertEquals(Bits.get(data, i), random.nextBoolean(), "Index: " + i); } }
public static byte[] shiftRight(final byte[] data, final int shift) { final int bytes = min(shift >>> 3, data.length); final int bits = shift & 7; if (bytes > 0) { for (int i = 0, n = data.length - bytes; i < n; ++i) { data[i] = data[i + bytes]; } for (int i = data.length, n = data.length - bytes; --i >= n;) { data[i] = (byte)0; } } if (bits > 0 && bytes < data.length) { int carry = 0; int nextCarry = 0; for (int i = data.length; --i >= 0;) { int d = data[i] & 0xFF; nextCarry = d << (Byte.SIZE - bits); d >>>= bits; d |= carry; data[i] = (byte)(d & 0xFF); carry = nextCarry; } } return data; }
Bits { public static byte[] shiftRight(final byte[] data, final int shift) { final int bytes = min(shift >>> 3, data.length); final int bits = shift & 7; if (bytes > 0) { for (int i = 0, n = data.length - bytes; i < n; ++i) { data[i] = data[i + bytes]; } for (int i = data.length, n = data.length - bytes; --i >= n;) { data[i] = (byte)0; } } if (bits > 0 && bytes < data.length) { int carry = 0; int nextCarry = 0; for (int i = data.length; --i >= 0;) { int d = data[i] & 0xFF; nextCarry = d << (Byte.SIZE - bits); d >>>= bits; d |= carry; data[i] = (byte)(d & 0xFF); carry = nextCarry; } } return data; } }
Bits { public static byte[] shiftRight(final byte[] data, final int shift) { final int bytes = min(shift >>> 3, data.length); final int bits = shift & 7; if (bytes > 0) { for (int i = 0, n = data.length - bytes; i < n; ++i) { data[i] = data[i + bytes]; } for (int i = data.length, n = data.length - bytes; --i >= n;) { data[i] = (byte)0; } } if (bits > 0 && bytes < data.length) { int carry = 0; int nextCarry = 0; for (int i = data.length; --i >= 0;) { int d = data[i] & 0xFF; nextCarry = d << (Byte.SIZE - bits); d >>>= bits; d |= carry; data[i] = (byte)(d & 0xFF); carry = nextCarry; } } return data; } private  Bits(); }
Bits { public static byte[] shiftRight(final byte[] data, final int shift) { final int bytes = min(shift >>> 3, data.length); final int bits = shift & 7; if (bytes > 0) { for (int i = 0, n = data.length - bytes; i < n; ++i) { data[i] = data[i + bytes]; } for (int i = data.length, n = data.length - bytes; --i >= n;) { data[i] = (byte)0; } } if (bits > 0 && bytes < data.length) { int carry = 0; int nextCarry = 0; for (int i = data.length; --i >= 0;) { int d = data[i] & 0xFF; nextCarry = d << (Byte.SIZE - bits); d >>>= bits; d |= carry; data[i] = (byte)(d & 0xFF); carry = nextCarry; } } return data; } private  Bits(); static boolean get(final byte[] data, final int index); static byte[] set(
		final byte[] data,
		final int index,
		final boolean value
	); static byte[] set(final byte[] data, final int index); static byte[] unset(final byte[] data, final int index); static void swap(
		final byte[] data, final int start, final int end,
		final byte[] otherData, final int otherStart
	); static int count(final byte[] data); static int count(final byte value); static byte[] shiftRight(final byte[] data, final int shift); static byte[] shiftLeft(final byte[] data, final int shift); static byte[] increment(final byte[] data); static byte[] invert(final byte[] data); static byte[] complement(final byte[] data); static byte[] flip(final byte[] data, final int index); static byte[] reverse(final byte[] array); static byte[] copy(final byte[] data, final int start, final int end); static boolean getAndSet(final byte[] array, final int index); static String toByteString(final byte... data); static byte[] fromByteString(final String data); static byte[] newArray(final int length); static byte[] newArray(final int length, final double p); static int toByteLength(final int bitLength); static int toInt(final byte[] data); static byte[] toBytes(final int value); static long toLong(final byte[] data); static byte[] toBytes(final long value); }
Bits { public static byte[] shiftRight(final byte[] data, final int shift) { final int bytes = min(shift >>> 3, data.length); final int bits = shift & 7; if (bytes > 0) { for (int i = 0, n = data.length - bytes; i < n; ++i) { data[i] = data[i + bytes]; } for (int i = data.length, n = data.length - bytes; --i >= n;) { data[i] = (byte)0; } } if (bits > 0 && bytes < data.length) { int carry = 0; int nextCarry = 0; for (int i = data.length; --i >= 0;) { int d = data[i] & 0xFF; nextCarry = d << (Byte.SIZE - bits); d >>>= bits; d |= carry; data[i] = (byte)(d & 0xFF); carry = nextCarry; } } return data; } private  Bits(); static boolean get(final byte[] data, final int index); static byte[] set(
		final byte[] data,
		final int index,
		final boolean value
	); static byte[] set(final byte[] data, final int index); static byte[] unset(final byte[] data, final int index); static void swap(
		final byte[] data, final int start, final int end,
		final byte[] otherData, final int otherStart
	); static int count(final byte[] data); static int count(final byte value); static byte[] shiftRight(final byte[] data, final int shift); static byte[] shiftLeft(final byte[] data, final int shift); static byte[] increment(final byte[] data); static byte[] invert(final byte[] data); static byte[] complement(final byte[] data); static byte[] flip(final byte[] data, final int index); static byte[] reverse(final byte[] array); static byte[] copy(final byte[] data, final int start, final int end); static boolean getAndSet(final byte[] array, final int index); static String toByteString(final byte... data); static byte[] fromByteString(final String data); static byte[] newArray(final int length); static byte[] newArray(final int length, final double p); static int toByteLength(final int bitLength); static int toInt(final byte[] data); static byte[] toBytes(final int value); static long toLong(final byte[] data); static byte[] toBytes(final long value);  }