I tried hard to find on google some reference about bitwise (flags)
native enums in Java 1.5, without success.
What's the proper way to define and compare bitwise enums (see below)?
Thank you!
public enum MyFlags
{
FlagA(1),
FlagB(2);
private final int bit;
MyFlags(int bit)
{
this.bit = bit;
}
public int getBit()
{
return bit;
}
}
...
MyFlags flags = FlagA + FlagB;
// Question: Is this bitwise comparison legal?
if((flags & FlagB) == FlagB)
System.out.println("Caught!");
else
System.out.println("Missed (should not happen).");
Bill Medland - 02 Oct 2006 17:52 GMT
> I tried hard to find on google some reference about bitwise (flags)
> native enums in Java 1.5, without success.
> What's the proper way to define and compare bitwise enums (see below)?
>
> Thank you!
I believe it is not possible. The new Enum concept in Java 1.5 is for true
enumerations and must be independent of representation.
> public enum MyFlags
> {
[quoted text clipped - 22 lines]
> else
> System.out.println("Missed (should not happen).");

Signature
Bill Medland
John W. Kennedy - 03 Oct 2006 02:45 GMT
> I believe it is not possible. The new Enum concept in Java 1.5 is for true
> enumerations and must be independent of representation.
That is not generally true. The inner structure of a Java 5.0 enum can
be entirely arbitrary.

Signature
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
Jeffrey Schwab - 03 Oct 2006 01:05 GMT
> I tried hard to find on google some reference about bitwise (flags)
> native enums in Java 1.5, without success.
[quoted text clipped - 28 lines]
> else
> System.out.println("Missed (should not happen).");
java.util.EnumSet may be what you want.
// cljp\MyFlags.java
package cljp;
public enum MyFlags {
FlagA,
FlagB
}
// cljp\Main.java
package cljp;
import java.util.EnumSet;
import static cljp.MyFlags.*;
public class Main {
public static void main(String[] args) {
EnumSet<MyFlags> flags = EnumSet.of(FlagA, FlagB);
if(flags.contains(FlagB)) {
System.out.println("Caught!");
} else {
System.out.println("Missed (should not happen).");
}
}
}
z-man - 03 Oct 2006 07:21 GMT
>> I tried hard to find on google some reference about bitwise (flags)
>> native enums in Java 1.5, without success.
[quoted text clipped - 58 lines]
> }
> }
Really interesting.
Thanks, Jeffrey!
John W. Kennedy - 03 Oct 2006 02:41 GMT
> I tried hard to find on google some reference about bitwise (flags)
> native enums in Java 1.5, without success.
[quoted text clipped - 28 lines]
> else
> System.out.println("Missed (should not happen).");
You seem to be addressing the task for which java.util.Bitset is designed.

Signature
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
z-man - 03 Oct 2006 07:24 GMT
>> I tried hard to find on google some reference about bitwise (flags)
>> native enums in Java 1.5, without success.
[quoted text clipped - 30 lines]
>
> You seem to be addressing the task for which java.util.Bitset is designed.
Cool!
many thanks, John.