为什么对 C 枚举定义中的值使用按位移位运算符?

发布于 2024-09-28 08:57:45 字数 824 浏览 4 评论 0原文

Apple 有时在其枚举定义中使用按位移位运算符。例如,在作为 Core Graphics 一部分的 CGDirectDisplay.h 文件中:

enum {
  kCGDisplayBeginConfigurationFlag  = (1 << 0),
  kCGDisplayMovedFlag           = (1 << 1),
  kCGDisplaySetMainFlag         = (1 << 2),
  kCGDisplaySetModeFlag         = (1 << 3),
  kCGDisplayAddFlag         = (1 << 4),
  kCGDisplayRemoveFlag          = (1 << 5),
  kCGDisplayEnabledFlag         = (1 << 8),
  kCGDisplayDisabledFlag        = (1 << 9),
  kCGDisplayMirrorFlag          = (1 << 10),
  kCGDisplayUnMirrorFlag        = (1 << 11),
  kCGDisplayDesktopShapeChangedFlag = (1 << 12)
};
typedef uint32_t CGDisplayChangeSummaryFlags;

为什么不像“普通”枚举int's强>?

Apple sometimes uses the Bitwise-Shift operator in their enum definitions. For example, in the CGDirectDisplay.h file which is part of Core Graphics:

enum {
  kCGDisplayBeginConfigurationFlag  = (1 << 0),
  kCGDisplayMovedFlag           = (1 << 1),
  kCGDisplaySetMainFlag         = (1 << 2),
  kCGDisplaySetModeFlag         = (1 << 3),
  kCGDisplayAddFlag         = (1 << 4),
  kCGDisplayRemoveFlag          = (1 << 5),
  kCGDisplayEnabledFlag         = (1 << 8),
  kCGDisplayDisabledFlag        = (1 << 9),
  kCGDisplayMirrorFlag          = (1 << 10),
  kCGDisplayUnMirrorFlag        = (1 << 11),
  kCGDisplayDesktopShapeChangedFlag = (1 << 12)
};
typedef uint32_t CGDisplayChangeSummaryFlags;

Why not simply use incrementing int's like in a "normal" enum?

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

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

发布评论

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

评论(9

望她远 2024-10-05 08:57:45

也许以十六进制(或二进制)写入值会有所帮助:-)

enum {
  kCGDisplayBeginConfigurationFlag  = (1 << 0), /* 0b0000000000000001 */
  kCGDisplayMovedFlag               = (1 << 1), /* 0b0000000000000010 */
  kCGDisplaySetMainFlag             = (1 << 2), /* 0b0000000000000100 */
  kCGDisplaySetModeFlag             = (1 << 3), /* 0b0000000000001000 */
  kCGDisplayAddFlag                 = (1 << 4), /* 0b0000000000010000 */
  kCGDisplayRemoveFlag              = (1 << 5), /* 0b0000000000100000 */
  kCGDisplayEnabledFlag             = (1 << 8), /* 0b0000000100000000 */
  kCGDisplayDisabledFlag            = (1 << 9), /* 0b0000001000000000 */
  kCGDisplayMirrorFlag              = (1 << 10),/* 0b0000010000000000 */
  kCGDisplayUnMirrorFlag            = (1 << 11),/* 0b0000100000000000 */
  kCGDisplayDesktopShapeChangedFlag = (1 << 12) /* 0b0001000000000000 */
};

现在您可以添加它们(或“或”它们)并获得不同的值

kCGDisplayAddFlag | kCGDisplayDisabledFlag /* 0b0000001000010000 */

Maybe writing the values in hexadecimal (or binary) helps :-)

enum {
  kCGDisplayBeginConfigurationFlag  = (1 << 0), /* 0b0000000000000001 */
  kCGDisplayMovedFlag               = (1 << 1), /* 0b0000000000000010 */
  kCGDisplaySetMainFlag             = (1 << 2), /* 0b0000000000000100 */
  kCGDisplaySetModeFlag             = (1 << 3), /* 0b0000000000001000 */
  kCGDisplayAddFlag                 = (1 << 4), /* 0b0000000000010000 */
  kCGDisplayRemoveFlag              = (1 << 5), /* 0b0000000000100000 */
  kCGDisplayEnabledFlag             = (1 << 8), /* 0b0000000100000000 */
  kCGDisplayDisabledFlag            = (1 << 9), /* 0b0000001000000000 */
  kCGDisplayMirrorFlag              = (1 << 10),/* 0b0000010000000000 */
  kCGDisplayUnMirrorFlag            = (1 << 11),/* 0b0000100000000000 */
  kCGDisplayDesktopShapeChangedFlag = (1 << 12) /* 0b0001000000000000 */
};

Now you can add them (or "or" them) and get different values

kCGDisplayAddFlag | kCGDisplayDisabledFlag /* 0b0000001000010000 */
半﹌身腐败 2024-10-05 08:57:45

通过这种方式,您可以将多个标志添加在一起以创建标志“集”,然后可以使用 & 找出任何给定标志是否在这样的集合中。

如果仅使用递增数字,则无法做到这一点。

例子:

int flags = kCGDisplayMovedFlag | kCGDisplaySetMainFlag; // 6
if(flags & kCGDisplayMovedFlag) {} // true
if(flags & kCGDisplaySetModeFlag) {} // not true

This way you can add multiple flags together to create a "set" of flags and can then use & to find out whether any given flag is in such a set.

You couldn't do that if it simply used incrementing numbers.

Example:

int flags = kCGDisplayMovedFlag | kCGDisplaySetMainFlag; // 6
if(flags & kCGDisplayMovedFlag) {} // true
if(flags & kCGDisplaySetModeFlag) {} // not true
时光匆匆的小流年 2024-10-05 08:57:45

C# 7 中的新功能终于添加了二进制文字,因此您可以将其编写为:

enum MyEnum
{
    kCGDisplayBeginConfigurationFlag  = 0b0000000000000001;
    kCGDisplayMovedFlag               = 0b0000000000000010;
    kCGDisplaySetMainFlag             = 0b0000000000000100;
    kCGDisplaySetModeFlag             = 0b0000000000001000;
    kCGDisplayAddFlag                 = 0b0000000000010000;
    kCGDisplayRemoveFlag              = 0b0000000000100000;
    kCGDisplayEnabledFlag             = 0b0000000001000000;
    kCGDisplayDisabledFlag            = 0b0000000010000000;
    kCGDisplayMirrorFlag              = 0b0000000100000000;
    kCGDisplayUnMirrorFlag            = 0b0000001000000000;
    kCGDisplayDesktopShapeChangedFlag = 0b0000010000000000;
};

如果您想让事情变得更简洁,您可以使用: _ 这也是 C# 7 的新功能,它允许您在数字中添加空格以使内容更具可读性,如下所示:

enum MyEnum
{
    kCGDisplayBeginConfigurationFlag  = 0b_0000_0000_0000_0001;
    kCGDisplayMovedFlag               = 0b_0000_0000_0000_0010;
    kCGDisplaySetMainFlag             = 0b_0000_0000_0000_0100;
    kCGDisplaySetModeFlag             = 0b_0000_0000_0000_1000;
    kCGDisplayAddFlag                 = 0b_0000_0000_0001_0000;
    kCGDisplayRemoveFlag              = 0b_0000_0000_0010_0000;
    kCGDisplayEnabledFlag             = 0b_0000_0000_0100_0000;
    kCGDisplayDisabledFlag            = 0b_0000_0000_1000_0000;
    kCGDisplayMirrorFlag              = 0b_0000_0001_0000_0000;
    kCGDisplayUnMirrorFlag            = 0b_0000_0010_0000_0000;
    kCGDisplayDesktopShapeChangedFlag = 0b_0000_0100_0000_0000;
};

使跟踪数字变得更加容易。

New in C# 7 is finally adding binary literals, so you can just write it as this:

enum MyEnum
{
    kCGDisplayBeginConfigurationFlag  = 0b0000000000000001;
    kCGDisplayMovedFlag               = 0b0000000000000010;
    kCGDisplaySetMainFlag             = 0b0000000000000100;
    kCGDisplaySetModeFlag             = 0b0000000000001000;
    kCGDisplayAddFlag                 = 0b0000000000010000;
    kCGDisplayRemoveFlag              = 0b0000000000100000;
    kCGDisplayEnabledFlag             = 0b0000000001000000;
    kCGDisplayDisabledFlag            = 0b0000000010000000;
    kCGDisplayMirrorFlag              = 0b0000000100000000;
    kCGDisplayUnMirrorFlag            = 0b0000001000000000;
    kCGDisplayDesktopShapeChangedFlag = 0b0000010000000000;
};

And if you want to make things even neater, you use this: _ which is also new to C# 7, which allows you to put spaces in numbers to make things more readable, like so:

enum MyEnum
{
    kCGDisplayBeginConfigurationFlag  = 0b_0000_0000_0000_0001;
    kCGDisplayMovedFlag               = 0b_0000_0000_0000_0010;
    kCGDisplaySetMainFlag             = 0b_0000_0000_0000_0100;
    kCGDisplaySetModeFlag             = 0b_0000_0000_0000_1000;
    kCGDisplayAddFlag                 = 0b_0000_0000_0001_0000;
    kCGDisplayRemoveFlag              = 0b_0000_0000_0010_0000;
    kCGDisplayEnabledFlag             = 0b_0000_0000_0100_0000;
    kCGDisplayDisabledFlag            = 0b_0000_0000_1000_0000;
    kCGDisplayMirrorFlag              = 0b_0000_0001_0000_0000;
    kCGDisplayUnMirrorFlag            = 0b_0000_0010_0000_0000;
    kCGDisplayDesktopShapeChangedFlag = 0b_0000_0100_0000_0000;
};

Makes it so much easier to keep track of the numbers.

擦肩而过的背影 2024-10-05 08:57:45

如果 FlagA=1、FlagB=2 且 FlagC=3,则 FlagA 或 FlagB 将给出与 FlagC 相同的值。移位运算符用于确保每个标志组合都是唯一的。

If you have FlagA=1, FlagB=2 and FlagC=3, FlagA or FlagB would give the same value as FlagC. The shift operator is used to ensure that every combination of flags is unique.

另类 2024-10-05 08:57:45

这将允许变量轻松组合多个标志:

unit32_t multFlag = kCGDisplayRemoveFlag | kCGDisplayMirrorFlag | kCGDisplaySetMainFlag'

This will allow for a variable to easily combine multiple flags:

unit32_t multFlag = kCGDisplayRemoveFlag | kCGDisplayMirrorFlag | kCGDisplaySetMainFlag'
青衫儰鉨ミ守葔 2024-10-05 08:57:45

我得到了一个带有标志的枚举,如下所示。

[Flags]
public enum PlatformOptions
{
    None = 0,
    Connectivity = 1,
    LiveVideo = 1 << 2,
    OnDeviceWithProbe = 1 << 3,
    ForceAnalyzeFeatureSet = 1 << 4,
    EnableGstreamerLiveADR = 1 << 5,
}

现在我已经得到了这样的价值。

var None = 0;
var Connectivity = 1;
var LiveVideo = 1 << 2;
var OnDeviceWithProbe = 1 << 3;
var ForceAnalyzeFeatureSet = 1 << 4;
var EnableGstreamerLiveADR = 1 << 5;

System.Console.WriteLine(None); // Returns 0
System.Console.WriteLine(Connectivity); // Returns 1
System.Console.WriteLine(LiveVideo); // Returns 4
System.Console.WriteLine(OnDeviceWithProbe); // Returns 8
System.Console.WriteLine(ForceAnalyzeFeatureSet); // Returns 16
System.Console.WriteLine(EnableGstreamerLiveADR); // Returns 32

I got an enum with flags as follows.

[Flags]
public enum PlatformOptions
{
    None = 0,
    Connectivity = 1,
    LiveVideo = 1 << 2,
    OnDeviceWithProbe = 1 << 3,
    ForceAnalyzeFeatureSet = 1 << 4,
    EnableGstreamerLiveADR = 1 << 5,
}

Now I have got its value like this.

var None = 0;
var Connectivity = 1;
var LiveVideo = 1 << 2;
var OnDeviceWithProbe = 1 << 3;
var ForceAnalyzeFeatureSet = 1 << 4;
var EnableGstreamerLiveADR = 1 << 5;

System.Console.WriteLine(None); // Returns 0
System.Console.WriteLine(Connectivity); // Returns 1
System.Console.WriteLine(LiveVideo); // Returns 4
System.Console.WriteLine(OnDeviceWithProbe); // Returns 8
System.Console.WriteLine(ForceAnalyzeFeatureSet); // Returns 16
System.Console.WriteLine(EnableGstreamerLiveADR); // Returns 32
鸵鸟症 2024-10-05 08:57:45

.. 因为 1<<7 看起来比 01000000 更简洁、更容易阅读。不是吗?

.. because 1<<7 looks more concise and easier to read than 01000000. Doesn't it?

守望孤独 2024-10-05 08:57:45

让我给你一个更多的练习例子。在 C++ 中,当您想要打开一个文件(打开以输出,并且在二进制模式而不是文本模式)时,您可以通过以下方式完成:

const char *filename = "/home/xy/test.bin";
fstream output(filename, ios::out | ios::binary);

您可以看到, ios::out | ios::binary 可以设置两种模式(打开输出、二进制模式)。

这是如何运作的?它是通过枚举(按位移位值):

enum _Ios_Openmode 
{ 
  _S_app        = 1L << 0,
  _S_ate        = 1L << 1,
  _S_bin        = 1L << 2,  /* 0b0000000000000100 */
  _S_in         = 1L << 3,
  _S_out        = 1L << 4,  /* 0b0000000000010000 */
  _S_trunc      = 1L << 5
  //.....
};

/// Perform input and output in binary mode (as opposed to text mode).
static const openmode binary =  _S_bin;

/// Open for input.  Default for @c ifstream and fstream.
static const openmode in =      _S_in;

/// Open for output.  Default for @c ofstream and fstream.
static const openmode out =     _S_out;

如果您在enum _Ios_Openmode中使用值递增1,则必须set(ios::out) 和 set(ios::binary) 执行两次。一次性查看和设置值可能不太方便。

Let's me give you a more practice example. In c++ when you want to open a file (Open for output, and in binary mode opposed to text mode), you can do it by:

const char *filename = "/home/xy/test.bin";
fstream output(filename, ios::out | ios::binary);

You can see, ios::out | ios::binary can set two mode(Open for output, and in binary mode).

How does this work ? It's by enum(bitwise-shift values):

enum _Ios_Openmode 
{ 
  _S_app        = 1L << 0,
  _S_ate        = 1L << 1,
  _S_bin        = 1L << 2,  /* 0b0000000000000100 */
  _S_in         = 1L << 3,
  _S_out        = 1L << 4,  /* 0b0000000000010000 */
  _S_trunc      = 1L << 5
  //.....
};

/// Perform input and output in binary mode (as opposed to text mode).
static const openmode binary =  _S_bin;

/// Open for input.  Default for @c ifstream and fstream.
static const openmode in =      _S_in;

/// Open for output.  Default for @c ofstream and fstream.
static const openmode out =     _S_out;

If you use value increment by 1 in enum _Ios_Openmode, you have to set(ios::out) and set(ios::binary) do two times. It may not so convenient to check and set value by one time.

抠脚大汉 2024-10-05 08:57:45

使用#define 更容易理解。但枚举可以将这些值组合在一起。

using #define is more understandable. but enum could group these value togater.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文