Bitwise (flags) enums: how to compare?

Z

z-man

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).");
 
B

Bill Medland

z-man said:
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.
 
J

Jeffrey Schwab

z-man said:
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).");

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).");
}
}
}
 
J

John W. Kennedy

z-man said:
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).");

You seem to be addressing the task for which java.util.Bitset is designed.
 
J

John W. Kennedy

Bill said:
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.
 
Z

z-man

z-man said:
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).");

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).");
}
}
}

Really interesting.
Thanks, Jeffrey!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,102
Members
48,773
Latest member
Kaybee

Latest Threads

Top