Updated BinUtil and CoordinatePacker

- Added packing 2 ints into a long to CoordinatePacker
- Added iPowerOf2(int) to BinUtil
This commit is contained in:
2020-08-06 21:36:45 +03:00
parent b9dc25096f
commit 1362dab2c1
6 changed files with 173 additions and 3 deletions

View File

@ -31,5 +31,9 @@ public class BinUtil {
public static int roundToGreaterPowerOf2(int x) {
return closestGreaterPowerOf2(x - 1);
}
public static boolean isPowerOf2(int x) {
return (x > 0) && ((x & (x - 1)) == 0);
}
}

View File

@ -22,6 +22,9 @@ public class CoordinatePacker {
private static final int BITS_3_INTS_INTO_LONG;
private static final long MASK_3_INTS_INTO_LONG;
private static final int BITS_2_INTS_INTO_LONG;
private static final long MASK_2_INTS_INTO_LONG;
static {
BITS_3_INTS_INTO_LONG = 64 / 3;
@ -37,7 +40,10 @@ public class CoordinatePacker {
* \_________/ - BITS_3_INTS_INTO_LONG ones - WIN
*/
MASK_3_INTS_INTO_LONG = (1 << BITS_3_INTS_INTO_LONG) - 1;
MASK_3_INTS_INTO_LONG = (1l << BITS_3_INTS_INTO_LONG) - 1;
BITS_2_INTS_INTO_LONG = 64 / 2;
MASK_2_INTS_INTO_LONG = (1l << BITS_2_INTS_INTO_LONG) - 1;
}
public static long pack3IntsIntoLong(int a, int b, int c) {
@ -65,5 +71,24 @@ public class CoordinatePacker {
return result;
}
public static long pack2IntsIntoLong(int a, int b) {
return
((a & MASK_2_INTS_INTO_LONG) << (1 * BITS_2_INTS_INTO_LONG)) |
((b & MASK_2_INTS_INTO_LONG) << (0 * BITS_2_INTS_INTO_LONG));
}
public static int unpack2IntsFromLong(long packed, int index) {
if (index < 0 || index >= 2) {
throw new IllegalArgumentException("Invalid index " + index);
}
int result = (int) (
(packed >>> ((1 - index) * BITS_2_INTS_INTO_LONG))
& MASK_2_INTS_INTO_LONG
);
return result;
}
}