You can use EnumSet . This allows you to use named bits and may be friendlier than using BitSet, which uses indexed bits.
A specialized implementation of Set for use with enumeration types. All elements in an enumeration set must come from the same enumeration type, which is specified, explicitly or implicitly, when creating the set. Enum sets are represented internally as bit vectors. This view is extremely compact and efficient. The space and running time of this class should be good enough to allow it to be used as a high-quality, typical alternative to traditional int-based "bit flags". Even bulk operations (such as containsAll and retainAll) must be performed very quickly if their argument is also an enumeration.
eg.
BitSet bs = new BitSet(4); bs.set(1); // READY bs.set(3); // LARGE_FLAG boolean largeFlag = bs.get(1); // LARGE_FLAG System.out.println("Using BitSet: "+bs); EnumSet<Settings> settings = EnumSet.noneOf(Settings.class); settings.add(Settings.READY); settings.add(Settings.LARGE_FLAG); boolean largeFlag2 = settings.contains(Settings.LARGE_FLAG); System.out.println("Using EnumSet: "+settings);
prints
Using BitSet: {1, 3} Using EnumSet: [READY, LARGE_FLAG]
IMHO EnumSet is much clearer if necessary.
source share