convert a two Byte bit mask into a EnumSet
bitmask, enumset, java
Solution
List<MyEnum> list = new ArrayList<MyEnum>();
for (MyEnum value : MyEnum.values()) {
if ((mask & (1 << value.ordinal())) != 0) {
list.add(value);
}
}
return EnumSet.copyOf(list);
For the 2 byte mask, combine the 2 bytes into one int. eg:
int mask = (((int)hbyte) & 0xff) << 8 | (((int)lbyte) & 0xff);
Problem
I am reading a binary file that has values stored in bit masks, both 1 Byte bit masks and 2 Byte bit masks. Each bit in the masks act as a switch that indicates where an Event has transpired. Example of 1 Byte mask: ``` 00000101 ``` Indicates that Event one and Event 3 has transpired. Example of Enum ``` public enum MyEnum { EventOne, EventTwo, ....; } ``` I have created a `Enum MyEnum`(as per Item 32 in Effective java, Second Edition) of the events. How can the binary bit masks be read into an `EnumSet<MyEnum>`?