Created
April 27, 2020 21:02
-
-
Save HoldYourWaffle/df9f4c00f2e51aaae55d36231bb3d6f5 to your computer and use it in GitHub Desktop.
Utility class for storing 64 booleans in a long
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
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
My version https://github.com/yujunliang/lambda/blob/master/src/main/java/algocraft/function/SixFlags.java