使用位掩码合并枚举值 [英] Combining Enum Value using Bitmask
问题描述
我知道可以在枚举值中使用位掩码,但是我不知道如何创建它。
I understand it's possible to use bitmasks in enum values, but I don't know how to create it.
我有一个简单的枚举:
enum State
{
minimizing = 0,
maximizing,
minimized,
maximized
};
状态始终为 State.minimized
或 State.maximized
,并且在调整大小时可以具有其他状态。因此可以最大化和最小化某些东西
A state is always State.minimized
or State.maximized
, and can have additional state on resize. So something can be Maximized and minimizing
推荐答案
我将假设 myState
具有您的枚举状态
的类型。
I am going to assume that myState
has the type of your enum State
.
枚举<的传统用法
用于创建此类型的变量可以采用的常量值。您希望将变量 myState
设置为枚举
中定义的值的组合。
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
.
枚举
将1、2、4和8定义为有效值,但您希望将变量设置为4 | 2 =6。尽管C对所有枚举
使用实现定义的 int
类型,但在C ++中并非如此。 myState = 6
在C ++中无效。实际上, myState = 4
在C ++中无效,您需要显式强制转换或使用 enum $ c的常量名称之一。 $ c>。
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
.
尽管在C语言中可能,但将 myState
设置为
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;
这样,您可以将字段写入 current
和 next
无关。
That way, you can write to the fields current
and next
undependently.
如果仍然需要位字段,则可以设置结构中的元素(以位为单位)。但是,这有点危险,位字段的实现取决于您的编译器。我什至不确定编译器是否接受在位字段中使用枚举类型(在C语言中应该没问题,因为 enum
s是 int
)。
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;
前面给出的解决方案当然是有效的。我的观点是,如果变量 myState
具有枚举状态
作为类型,则它应仅使用枚举
为其值,而不是组合。
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.
也许 myState
还有另一种类型,我怎么知道。
Maybe myState
has another type, what do I know.
如果 myState
不是枚举状态
类型,则可以组合使用在 enum
中定义的常量。
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?
这篇关于使用位掩码合并枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!