mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-01-20 09:17:29 +07:00
Committing BitUtils and SafeUnsigned
This commit is contained in:
parent
e3fc03f240
commit
d8679e34e1
27
core/src/com/riiablo/io/BitUtils.java
Normal file
27
core/src/com/riiablo/io/BitUtils.java
Normal 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);
|
||||
}
|
||||
}
|
22
core/src/com/riiablo/io/SafeUnsigned.java
Normal file
22
core/src/com/riiablo/io/SafeUnsigned.java
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user