Using long as bit mask


Checking, setting, clearing, and toggling individual bits. Using long as bit mask

Assuming we want to modify bit n of an integer primitive, i (byte, short, char, int, or long):

(i & 1 << n) != 0 // checks bit 'n'
i |= 1 << n; // sets bit 'n' to 1
i &= ~(1 << n); // sets bit 'n' to 0
i ^= 1 << n; // toggles the value of bit 'n'

Using long/int/short/byte as a bit mask:

public class BitMaskExample {
    private static final long BIT_1 = 1L << 0;
    private static final long BIT_2 = 1L << 1;
    private static final long BIT_3 = 1L << 2;
    private static final long BIT_4 = 1L << 3;
    private static final long BIT_5 = 1L << 4;
    private static final long BIT_60 = 1L << 59;
 
    public static void main(String[] args) {
        checkBitMask(BIT_1 | BIT_3 | BIT_5 | BIT_60);
    }
 
    private static void checkBitMask(long bitmask) {
        System.out.println("BIT_1: " + ((bitmask & BIT_1) != 0));
        System.out.println("BIT_2: " + ((bitmask & BIT_2) != 0));
        System.out.println("BIT_3: " + ((bitmask & BIT_3) != 0));
        System.out.println("BIT_4: " + ((bitmask & BIT_4) != 0));
        System.out.println("BIT_5: " + ((bitmask & BIT_5) != 0));
        System.out.println("BIT_60: " + ((bitmask & BIT_60) != 0));
    }
}

Result:

BIT_1: true
BIT_2: false
BIT_3: true
BIT_4: false
BIT_5: true
BIT_60: true

which matches that mask we passed as checkBitMask parameter: BIT_1 | BIT_3 | BIT_5 | BIT_60


java.util.BitSet class

Since 1.7 there's a java.util.BitSet class that provides simple and user-friendly bit storage and manipulation interface:

final BitSet set1 = new BitSet(8); // by default all bits are unset
 
IntStream.range(0, 8).filter(i -> i % 2 == 0).forEach(set1::set); // {0, 2, 4, 6}
 
set1.set(3); // {0, 2, 3, 4, 6}
 
set1.set(3, false); // {0, 2, 4, 6}
 
final boolean check1 = set1.get(3); // check1 = false
 
set1.flip(6); // {0, 2, 4}
 
set1.set(100); // {0, 2, 4, 100} - expands automatically

BitSet implements Clonable and Serializable, and under the hood all bit values are stored in long[] words field, that expands automatically.

It also supports whole-set logical operations and, or, xor, andNot:

final BitSet setA = new BitSet(8);
final BitSet setB = new BitSet(8);
final BitSet setC = new BitSet(8);
final BitSet setD = new BitSet(8);
 
setA.and(new BitSet(8)); // Performs AND operation with an empty BitSet
setB.or(new BitSet(8)); // Performs OR operation with an empty BitSet
setC.xor(new BitSet(8)); // Performs XOR operation with an empty BitSet
setD.andNot(new BitSet(8)); // Performs AND-NOT operation with an empty BitSet

Basic Programs