|= 和 &= 如何工作?

发布于 2024-11-15 22:01:10 字数 235 浏览 2 评论 0原文

我想以这样一种方式进行枚举: 我可以简单地这样

做:

enum Things
{
   Something,
SomethingElse
};

或者我必须小心地给他们特定的数字才能使其工作?

另外,如果一个值,比如 3,已经被 |= 写入,那么 |= 它会再次导致故障(如 if(attributes & 3) 将不再起作用)

谢谢

I want to make an enum in such a way that I can & parameters after |ing them in.

Can I simply do:

enum Things
{
   Something,
SomethingElse
};

Or must I be careful to give them specific numbers for this to work?

Also if a value, like say, 3, has already been |='d in, would |= it in again cause this to malfunction (as in, if(attributes & 3) would no longer work)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜血缘 2024-11-22 22:01:10

然后将您的枚举定义为:

enum Things
{
   Something = 1       //0000 0001
   SomethingElse = 2;  //0000 0010
   SomethingX = 4;     //0000 0100
   SomethingY = 8;     //0000 1000
   SomethingZ = 16;    //0001 0000
};

这个想法是,二进制表示中只有一位应该是 1,其他应该是 0,如上面的注释所示。另外,我在注释中仅使用了 8 位,这并不意味着枚举值是一个字节大小。我只是为了方便才使用它们。枚举值可以非常大,甚至可以容纳long

Then define your enum as:

enum Things
{
   Something = 1       //0000 0001
   SomethingElse = 2;  //0000 0010
   SomethingX = 4;     //0000 0100
   SomethingY = 8;     //0000 1000
   SomethingZ = 16;    //0001 0000
};

The idea is, only one bit in the binary representation should be 1, others should be 0, as shown in the comments above. Also, I used 8 bits only in the comment, that doesn't mean that the enum values are one byte size. I only used them for convenience. Enum values can be very large, can hold even long.

断念 2024-11-22 22:01:10

您必须使用特殊值,即 2 的幂 (1, 2, 4, 8, 16, ...)。
在进一步研究这个“|和&功能”之前,你可能应该阅读这篇文章:
http://en.wikipedia.org/wiki/Binary_numeral_system

这也可能值得一读:< a href="http://en.wikipedia.org/wiki/Flag_(计算" rel="nofollow">http://en.wikipedia.org/wiki/Flag_(计算)

You have to use special values, namely powers of two (1, 2, 4, 8, 16, ...).
And you should probably read this before investigating this "| and & feature" any further:
http://en.wikipedia.org/wiki/Binary_numeral_system

This might also be worth reading: http://en.wikipedia.org/wiki/Flag_(computing)

A君 2024-11-22 22:01:10

当您利用计算机的本机二进制编码时,使用 |& 打包标志效果最佳。

因此,使用 2 的幂(此处采用十进制表示形式):

enum Things
{
   Something     = 1,
   SomethingElse = 2,
   SomethingMore = 4,
   SomethingHuge = 8
};

这使得每个标志都可以唯一用整数的一位来表示,从而允许单独激活和停用每个标志。

结果是:

char x = 0;
x |=  Something;     // x in binary looks like 00000001
x |=  SomethingMore; // x in binary looks like 00001001
x &= ~Something;     // x in binary looks like 00001000

我希望这是有道理的。

Packing flags with | and & works best when you take advantage of the computer's native binary encoding.

So, using the powers of 2 (here in decimal representation):

enum Things
{
   Something     = 1,
   SomethingElse = 2,
   SomethingMore = 4,
   SomethingHuge = 8
};

This enables each flag to be represented exclusively in a single bit of an integer, allowing each one to be activated and de-activated individually.

The result is:

char x = 0;
x |=  Something;     // x in binary looks like 00000001
x |=  SomethingMore; // x in binary looks like 00001001
x &= ~Something;     // x in binary looks like 00001000

I hope that this makes sense.

凯凯我们等你回来 2024-11-22 22:01:10

如果要对 Enum 使用按位运算,则值必须是 2 的幂。这通常称为 位字段

If you want to use bit-wise operations with an Enum, the values must be a power of 2. This is commonly referred to as a Bit Field.

云雾 2024-11-22 22:01:10

您可以使其变得更容易:

enum Things
{
   Something =     0x00000001   //0000 0001
   SomethingElse = 0x00000002;  //0000 0010
   SomethingX    = 0x00000004;  //0000 0100
   SomethingY    = 0x00000008;  //0000 1000
   SomethingZ    = 0x00000010;  //0001 0000 // instead of 16
   SomethingZ2   = 0x00000020;  //0010 0000 // instead of 32
   SomethingZ3   = 0x00000040;  //0100 0000 // instead of 64
   SomethingZ4   = 0x00000080;  //1000 0000 // instead of 128
};

正如纳瓦兹所说,如果您使用十六进制代码,或者甚至更好地使用宏,

#define BITMASK(x) (1<<(x))

enum Things
{
   Something =    BITMASK(0)   //0000 0001
   SomethingElse = BITMASK(1) ;  //0000 0010
   SomethingX    = BITMASK(2) ;  //0000 0100
   SomethingY    = BITMASK(3) ;  //0000 1000
   SomethingZ    = BITMASK(4) ;  //0001 0000 
};

As Nawaz said and you can make it easier if you use a hexadecimal codes

enum Things
{
   Something =     0x00000001   //0000 0001
   SomethingElse = 0x00000002;  //0000 0010
   SomethingX    = 0x00000004;  //0000 0100
   SomethingY    = 0x00000008;  //0000 1000
   SomethingZ    = 0x00000010;  //0001 0000 // instead of 16
   SomethingZ2   = 0x00000020;  //0010 0000 // instead of 32
   SomethingZ3   = 0x00000040;  //0100 0000 // instead of 64
   SomethingZ4   = 0x00000080;  //1000 0000 // instead of 128
};

Or even better use a macro:

#define BITMASK(x) (1<<(x))

enum Things
{
   Something =    BITMASK(0)   //0000 0001
   SomethingElse = BITMASK(1) ;  //0000 0010
   SomethingX    = BITMASK(2) ;  //0000 0100
   SomethingY    = BITMASK(3) ;  //0000 1000
   SomethingZ    = BITMASK(4) ;  //0001 0000 
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文