Thread: Bit mask enum

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006

    Bit mask enum


    Since I guess there's no enumeration method for creating bit mask constants, I have created a macro:

    #define BM_ENUM(x)					\
    	x##_ENUM_IDX,					\
    	x = 1 << x##_ENUM_IDX,				\
    	x##_ENUM_IDX_AGAIN = x##_ENUM_IDX
    enum TestBitmask {
    What do you think of it? I don't understand why there wasn't a bit mask enumeration method built in to the language from the beginning, it could have been useful.
    Come on, you can do it! b( ~_')

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    enum's can be used as bitmasks. Yours is (apparently) trying to do both the bitnumber enumeration and the bitvalues in the same enum, which is a bit odd, but that's obviously your choice (there's no "law" agains repeating the same enum value - although the debugger will show the first one to match, which may not be what you expected).

    Since enum values can be declared as you wish, there's really no need to have a special variant that does enum's for bitmasks. There are some languages that attempt to be "swiss army knifes", but C is a rather spartan language, and as long as the constructs can be used to do what you want, then I don't see why there should be another way of doing the same thing.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Toronto, ON
    That code is extremely confusing.
    I don't see what's so hard about writing this:
    enum TestBitmask {
        First  = 0x01,
        Second = 0x02,
        Third  = 0x04,
        Fourth = 0x08,
        Fifth  = 0x10,
        Sixth  = 0x20
    At least then you can look at the code to see what value matches with which enum name, rather than doing some crazy calculation in your head... It's just simple Hex.

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    Theoretically, you could just do
    enum TestBitmask {
        First  = 1,
        Second = 1 << 1,
        Third  = 1 << 2,
        Fourth = 1 << 3,
        Fifth  = 1 << 4,
        Sixth  = 1 << 5
    Again, no confusion. And you don't have to calculate hex either if you absolutely don't want to.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    Quote Originally Posted by TriKri View Post
    What do you think of it? I don't understand why there wasn't a bit mask enumeration method built in to the language from the beginning, it could have been useful.
    1. It defeats the purpose of enum. An enumeration is a way to give names to the valid values. Yet by combining masks, you get a value that is not equal to any value in the enumeration. A person might therefore assume that the resulting combination is not a valid value. Don't try to trick yourself. A bitwise combination is an integer, not an enum.

    2. It only works in C. C++ rejects this kind of thing.

    Just do what the rest of the world does. #define your mask constants.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit to 64 bit Ubuntu
    By Akkernight in forum Tech Board
    Replies: 15
    Last Post: 11-17-2008, 03:14 AM
  2. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  3. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM
  4. Replies: 7
    Last Post: 12-10-2004, 08:18 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM