-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Committing BitUtils and SafeUnsigned
- Loading branch information
1 parent
e3fc03f
commit d8679e3
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |