Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining Enum Value using Bitmask

Tags:

c++

c

enums

bitmask

I understand it's possible to use bitmasks in enum values, but I don't know how to create it.

I have a simple enum :

enum State
{
    minimizing = 0,
    maximizing,

    minimized,
    maximized
};

A state is always State.minimized or State.maximized, and can have additional state on resize. So something can be Maximized and minimizing

like image 665
Thomas Joulin Avatar asked Feb 07 '11 11:02

Thomas Joulin


People also ask

Can you Bitmask an enum?

A bitmask enum attribute macro, to turn an enum into a bitmask. A bitmask can have (un)signed integer types, the default type is usize . First created because I wanted something simple, evolved with inspiration from the bitflags crate, which might be something you want to take a look at.

Can two enums have the same value?

Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.

Can you add values to enum?

You can add a new value to a column of data type enum using ALTER MODIFY command. If you want the existing value of enum, then you need to manually write the existing enum value at the time of adding a new value to column of data type enum.

How do I use a bitmask?

In a bit mask, Bitwise AND can be used to make sure particular bits in the result value are set to 0. The trick is to put a 1 in the mask for any bit you do not want changed in the result, and a 0 in the mask for any bit that you want to make sure is a 0 in the result.


2 Answers

I am going to assume that myState has the type of your enum State.

The traditional use of enum is to create the constant values that a variable of this type can take. You wish to set you variable myState to a combination of the values defined in the enum.

The enum defines 1, 2, 4, and 8 as valid values, yet you want to be able to set the variable to 4 | 2 = 6. While C uses your implementation-defined int type for all enum, it is not the case in C++. myState = 6 is not valid in C++. Actually, myState = 4 is not either valid in C++, you need to cast explicitly or use one of the constant names of the enum.

Although possible in C, it is not good practice to set myState to a value that is not defined by its type (for example to 6).

In your case, a solution that seems consequent would be:

typedef enum {
    OTHER,
    MINIMIZED,
    MAXIMIZED
} win_size_t;

typedef struct {
    win_size_t current;
    win_size_t next;
} state_t;

state_t myState;

That way, you can write to the fields current and next undependently.

If you still want to have bit fields, you can set the size of the elements of your struct in bits. It is kind of dangerous though, the implementation of bit fields depend on your compiler. I am not even sure if compilers would accept to have an enum type in a bit field (should be ok in C, since enums are int).

typedef struct {
    win_size_t current : 2;  // not tested
    win_size_t next : 2;
} state_t;

The previous given solutions are valid of course. My point is that if your variable myState has your enum State as type, it should only use the members of the enum for its values, not a combination.

Maybe myState has another type, what do I know.


If myState is not of the enum State type, then you may use the constants defined in your enum in combination.

enum State {
    MINIMIZING = (1u << 0),
    MAXIMIZING = (1u << 1),
    MINIMIZED  = (1u << 2),
    MAXIMIZED  = (1u << 3),
};

unsigned int myState = 0;

myState |= MAXIMIZED;  // sets that bit
myState &= ~MAXIMIZED; // resets that bit

This allows you to do two things in one assignment:

myState = MAXIMIZED | MINIMIZING;

But also things you are not likely to want:

myState = MAXIMIZED | MINIMIZED;  // does that make sense?
like image 90
Gauthier Avatar answered Sep 22 '22 00:09

Gauthier


Use a different bit for every value in your enumeration, such as:

enum State 
{
  minimizing = 0x01, // 00000001
  maximizing = 0x02, // 00000010
  minimized  = 0x04, // 00000100
  maximized  = 0x08  // 00001000
}:

Then, you can combine multiple values with bitwise or (minimizing | maximized) and test for values with bitwise and (bool is_minimized = (flags & minimized);).

like image 42
Victor Nicollet Avatar answered Sep 23 '22 00:09

Victor Nicollet