Created
June 29, 2023 13:49
-
-
Save audinue/247eacadd8723426f5bef0639166d9b6 to your computer and use it in GitHub Desktop.
Java bitmask classes.
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 ByteBitmask { | |
public static final byte MASK_0 = 1 << 0; | |
public static final byte MASK_1 = 1 << 1; | |
public static final byte MASK_2 = 1 << 2; | |
public static final byte MASK_3 = 1 << 3; | |
public static final byte MASK_4 = 1 << 4; | |
public static final byte MASK_5 = 1 << 5; | |
public static final byte MASK_6 = 1 << 6; | |
public static final byte MASK_7 = (byte) (1 << 7); | |
private byte value; | |
public void clear() { | |
value = 0; | |
} | |
public void add(byte mask) { | |
value |= mask; | |
} | |
public void remove(byte mask) { | |
value &= ~mask; | |
} | |
public boolean contains(byte mask) { | |
return (value & mask) == mask; | |
} | |
public static byte clear(byte value) { | |
return 0; | |
} | |
public static byte add(byte value, byte mask) { | |
return (byte) (value | mask); | |
} | |
public static byte remove(byte value, byte mask) { | |
return (byte) (value & ~mask); | |
} | |
public static boolean contains(byte value, byte mask) { | |
return (value & mask) == mask; | |
} | |
} |
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 IntBitmask { | |
public static final int MASK_0 = 1 << 0; | |
public static final int MASK_1 = 1 << 1; | |
public static final int MASK_2 = 1 << 2; | |
public static final int MASK_3 = 1 << 3; | |
public static final int MASK_4 = 1 << 4; | |
public static final int MASK_5 = 1 << 5; | |
public static final int MASK_6 = 1 << 6; | |
public static final int MASK_7 = 1 << 7; | |
public static final int MASK_8 = 1 << 8; | |
public static final int MASK_9 = 1 << 9; | |
public static final int MASK_10 = 1 << 10; | |
public static final int MASK_11 = 1 << 11; | |
public static final int MASK_12 = 1 << 12; | |
public static final int MASK_13 = 1 << 13; | |
public static final int MASK_14 = 1 << 14; | |
public static final int MASK_15 = 1 << 15; | |
public static final int MASK_16 = 1 << 16; | |
public static final int MASK_17 = 1 << 17; | |
public static final int MASK_18 = 1 << 18; | |
public static final int MASK_19 = 1 << 19; | |
public static final int MASK_20 = 1 << 20; | |
public static final int MASK_21 = 1 << 21; | |
public static final int MASK_22 = 1 << 22; | |
public static final int MASK_23 = 1 << 23; | |
public static final int MASK_24 = 1 << 24; | |
public static final int MASK_25 = 1 << 25; | |
public static final int MASK_26 = 1 << 26; | |
public static final int MASK_27 = 1 << 27; | |
public static final int MASK_28 = 1 << 28; | |
public static final int MASK_29 = 1 << 29; | |
public static final int MASK_30 = 1 << 30; | |
public static final int MASK_31 = 1 << 31; | |
private int value; | |
public void clear() { | |
value = 0; | |
} | |
public void add(int mask) { | |
value |= mask; | |
} | |
public void remove(int mask) { | |
value &= ~mask; | |
} | |
public boolean contains(int mask) { | |
return (value & mask) == mask; | |
} | |
public static int clear(int value) { | |
return 0; | |
} | |
public static int add(int value, int mask) { | |
return value | mask; | |
} | |
public static int remove(int value, int mask) { | |
return value & ~mask; | |
} | |
public static boolean contains(int value, int mask) { | |
return (value & mask) == mask; | |
} | |
} |
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 LongBitmask { | |
public static final long MASK_0 = 1L << 0L; | |
public static final long MASK_1 = 1L << 1L; | |
public static final long MASK_2 = 1L << 2L; | |
public static final long MASK_3 = 1L << 3L; | |
public static final long MASK_4 = 1L << 4L; | |
public static final long MASK_5 = 1L << 5L; | |
public static final long MASK_6 = 1L << 6L; | |
public static final long MASK_7 = 1L << 7L; | |
public static final long MASK_8 = 1L << 8L; | |
public static final long MASK_9 = 1L << 9L; | |
public static final long MASK_10 = 1L << 10L; | |
public static final long MASK_11 = 1L << 11L; | |
public static final long MASK_12 = 1L << 12L; | |
public static final long MASK_13 = 1L << 13L; | |
public static final long MASK_14 = 1L << 14L; | |
public static final long MASK_15 = 1L << 15L; | |
public static final long MASK_16 = 1L << 16L; | |
public static final long MASK_17 = 1L << 17L; | |
public static final long MASK_18 = 1L << 18L; | |
public static final long MASK_19 = 1L << 19L; | |
public static final long MASK_20 = 1L << 20L; | |
public static final long MASK_21 = 1L << 21L; | |
public static final long MASK_22 = 1L << 22L; | |
public static final long MASK_23 = 1L << 23L; | |
public static final long MASK_24 = 1L << 24L; | |
public static final long MASK_25 = 1L << 25L; | |
public static final long MASK_26 = 1L << 26L; | |
public static final long MASK_27 = 1L << 27L; | |
public static final long MASK_28 = 1L << 28L; | |
public static final long MASK_29 = 1L << 29L; | |
public static final long MASK_30 = 1L << 30L; | |
public static final long MASK_31 = 1L << 31L; | |
public static final long MASK_32 = 1L << 32L; | |
public static final long MASK_33 = 1L << 33L; | |
public static final long MASK_34 = 1L << 34L; | |
public static final long MASK_35 = 1L << 35L; | |
public static final long MASK_36 = 1L << 36L; | |
public static final long MASK_37 = 1L << 37L; | |
public static final long MASK_38 = 1L << 38L; | |
public static final long MASK_39 = 1L << 39L; | |
public static final long MASK_40 = 1L << 40L; | |
public static final long MASK_41 = 1L << 41L; | |
public static final long MASK_42 = 1L << 42L; | |
public static final long MASK_43 = 1L << 43L; | |
public static final long MASK_44 = 1L << 44L; | |
public static final long MASK_45 = 1L << 45L; | |
public static final long MASK_46 = 1L << 46L; | |
public static final long MASK_47 = 1L << 47L; | |
public static final long MASK_48 = 1L << 48L; | |
public static final long MASK_49 = 1L << 49L; | |
public static final long MASK_50 = 1L << 50L; | |
public static final long MASK_51 = 1L << 51L; | |
public static final long MASK_52 = 1L << 52L; | |
public static final long MASK_53 = 1L << 53L; | |
public static final long MASK_54 = 1L << 54L; | |
public static final long MASK_55 = 1L << 55L; | |
public static final long MASK_56 = 1L << 56L; | |
public static final long MASK_57 = 1L << 57L; | |
public static final long MASK_58 = 1L << 58L; | |
public static final long MASK_59 = 1L << 59L; | |
public static final long MASK_60 = 1L << 60L; | |
public static final long MASK_61 = 1L << 61L; | |
public static final long MASK_62 = 1L << 62L; | |
public static final long MASK_63 = 1L << 63L; | |
private long value; | |
public void clear() { | |
value = 0; | |
} | |
public void add(long mask) { | |
value |= mask; | |
} | |
public void remove(long mask) { | |
value &= ~mask; | |
} | |
public boolean contains(long mask) { | |
return (value & mask) == mask; | |
} | |
public static long clear(long value) { | |
return 0; | |
} | |
public static long add(long value, long mask) { | |
return value | mask; | |
} | |
public static long remove(long value, long mask) { | |
return value & ~mask; | |
} | |
public static boolean contains(long value, long mask) { | |
return (value & mask) == mask; | |
} | |
} |
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 ShortBitmask { | |
public static final short MASK_0 = 1 << 0; | |
public static final short MASK_1 = 1 << 1; | |
public static final short MASK_2 = 1 << 2; | |
public static final short MASK_3 = 1 << 3; | |
public static final short MASK_4 = 1 << 4; | |
public static final short MASK_5 = 1 << 5; | |
public static final short MASK_6 = 1 << 6; | |
public static final short MASK_7 = 1 << 7; | |
public static final short MASK_8 = 1 << 8; | |
public static final short MASK_9 = 1 << 9; | |
public static final short MASK_10 = 1 << 10; | |
public static final short MASK_11 = 1 << 11; | |
public static final short MASK_12 = 1 << 12; | |
public static final short MASK_13 = 1 << 13; | |
public static final short MASK_14 = 1 << 14; | |
public static final short MASK_15 = (short) (1 << 15); | |
private short value; | |
public void clear() { | |
value = 0; | |
} | |
public void add(short mask) { | |
value |= mask; | |
} | |
public void remove(short mask) { | |
value &= ~mask; | |
} | |
public boolean contains(short mask) { | |
return (value & mask) == mask; | |
} | |
public static short clear(short value) { | |
return 0; | |
} | |
public static short add(short value, short mask) { | |
return (short) (value | mask); | |
} | |
public static short remove(short value, short mask) { | |
return (short) (value & ~mask); | |
} | |
public static boolean contains(short value, short mask) { | |
return (value & mask) == mask; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment