Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Bitwise (flags) enums: how to compare?

Thread view: 
z-man - 02 Oct 2006 09:38 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!

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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.