Skip to content

Instantly share code, notes, and snippets.

@HoldYourWaffle
Created April 27, 2020 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save HoldYourWaffle/df9f4c00f2e51aaae55d36231bb3d6f5 to your computer and use it in GitHub Desktop.
Save HoldYourWaffle/df9f4c00f2e51aaae55d36231bb3d6f5 to your computer and use it in GitHub Desktop.
Utility class for storing 64 booleans in a long
public class BitBoolean {
private static final long[] indexToBitMask = new long[64];
static {
long mask = 1;
for (int i=0; i<64; i++) {
indexToBitMask[i] = mask;
mask *= 2;
}
}
public static long TRUE = -1; // all 1's in binary
public static long FALSE = 0;
public static long setTrue(long container, int index) {
return container | indexToBitMask[index];
}
public static long setFalse(long container, int index) {
return container & (~indexToBitMask[index]);
}
public static boolean get(long container, int index) {
return ((container >> index) & 1) == 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment