Skip to content

Commit

Permalink
Committing BitUtils and SafeUnsigned
Browse files Browse the repository at this point in the history
  • Loading branch information
collinsmith committed Aug 11, 2020
1 parent e3fc03f commit d8679e3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
27 changes: 27 additions & 0 deletions core/src/com/riiablo/io/BitUtils.java
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions core/src/com/riiablo/io/SafeUnsigned.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit d8679e3

Please sign in to comment.