diff --git a/core/src/com/riiablo/io/BitUtils.java b/core/src/com/riiablo/io/BitUtils.java new file mode 100644 index 00000000..d22f4b48 --- /dev/null +++ b/core/src/com/riiablo/io/BitUtils.java @@ -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); + } +} diff --git a/core/src/com/riiablo/io/SafeUnsigned.java b/core/src/com/riiablo/io/SafeUnsigned.java new file mode 100644 index 00000000..9333003a --- /dev/null +++ b/core/src/com/riiablo/io/SafeUnsigned.java @@ -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; + } +}