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
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.
Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.
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.
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.
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 enum
s 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?
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);
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With