Created readRaw(int) and added support for correctly reading signed 64 bits

This commit is contained in:
Collin Smith 2020-08-03 16:12:52 -07:00
parent d1ee547530
commit af480bdfc3

View File

@ -115,6 +115,10 @@ public class BitStream {
return readBoolean() ? 1 : 0;
}
public long readRaw(int bits) {
return readUnsigned(bits);
}
public long readUnsigned(int bits) {
Validate.inclusiveBetween(0, Long.SIZE, bits, "bits must be in range [0,64]");
if (bits == 0) return 0;
@ -161,6 +165,10 @@ public class BitStream {
}
public long readSigned(int bits) {
if (bits == Long.SIZE) {
return readRaw(bits);
}
final int shift = Long.SIZE - bits;
return readUnsigned(bits) << shift >> shift;
}