为什么对C枚举定义中的值使用Bitwise-Shift运算符?

cpjpxq1n  于 2022-12-02  发布在  其他
关注(0)|答案(9)|浏览(126)

Apple有时会在其枚举定义中使用Bitwise-Shift运算符。例如,在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;

为什么不像在“normal”enum中那样简单地使用递增的int

pprl5pva

pprl5pva1#

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

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 */
pw9qyyiw

pw9qyyiw2#

通过这种方式,您可以将多个标志添加到一起以创建一个标志“集”,然后可以使用&来查找任何给定的标志是否在这样的集中。
如果它只是使用递增的数字,你就无法做到这一点。
示例:

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

jhkqcmku3#

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;
};

这样就更容易追踪号码了。

ivqmmu1c

ivqmmu1c4#

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

neekobn8

neekobn85#

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

unit32_t multFlag = kCGDisplayRemoveFlag | kCGDisplayMirrorFlag | kCGDisplaySetMainFlag'
5t7ly7z5

5t7ly7z56#

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

jchrr9hc

jchrr9hc7#

让我给予你一个更实际的例子。在C++中,当你想打开一个文件(打开输出,以二进制模式而不是文本模式),你可以这样做:

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

可以看到,ios::out | ios::binary可以设置两种模式(Open for output和in binary mode)。

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

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)执行两次,一次检查和设置值可能不太方便

mklgxw1f

mklgxw1f8#

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

[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
xqk2d5yq

xqk2d5yq9#

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

相关问题