Committing BitUtils and SafeUnsigned

This commit is contained in:
Collin Smith 2020-08-11 01:07:35 -07:00
parent e3fc03f240
commit d8679e34e1
2 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,27 @@
package com.riiablo.io;
public class BitUtils {
private BitUtils() {}
public static boolean isUnsigned(long value, int bits) {
assert 0 < bits : "bits(" + bits + ") < " + 0;
assert bits <= Long.SIZE : "bits(" + bits + ") > " + Long.SIZE;
return (value & (1 << (bits - 1))) == 0;
}
public static boolean isUnsigned(byte value) {
return isUnsigned(value, Byte.SIZE);
}
public static boolean isUnsigned(short value) {
return isUnsigned(value, Short.SIZE);
}
public static boolean isUnsigned(int value) {
return isUnsigned(value, Integer.SIZE);
}
public static boolean isUnsigned(long value) {
return isUnsigned(value, Long.SIZE);
}
}

View File

@ -0,0 +1,22 @@
package com.riiablo.io;
public class SafeUnsigned extends RuntimeException {
public final long value;
SafeUnsigned(long value) {
super("value(" + value + ") is not unsigned!");
this.value = value;
}
public short u8() {
return (short) value;
}
public int u16() {
return (int) value;
}
public long u32() {
return (long) value;
}
}