像'1 << 0'这样的typedef枚举语法是什么意思?

MikeyE

我有点熟悉C和C ++的typedef枚举语法。我现在正在使用Objective-C进行编程,并在以下示例中遇到了语法。我不确定语法是否特定于Objective-C。但是,我的问题在以下代码段中,语法1 << 0是什么意思?

typedef enum {
   CMAttitudeReferenceFrameXArbitraryZVertical = 1 << 0,
   CMAttitudeReferenceFrameXArbitraryCorrectedZVertical = 1 << 1,
   CMAttitudeReferenceFrameXMagneticNorthZVertical = 1 << 2,
   CMAttitudeReferenceFrameXTrueNorthZVertical = 1 << 3
} CMAttitudeReferenceFrame;
埃里克·贾布洛(Eric Jablow)

这是C语言家族所共有的,并且在C,C ++和Objective-C中均相同。与Java,Pascal和类似语言不同,C枚举不限于为其命名的值;它实际上是一种整数类型,其大小可以表示所有命名值,并且可以将枚举类型的变量设置为枚举成员中的算术表达式。通常,一个人使用移位将值乘以2,而一个人使用按位逻辑运算来组合值。

typedef enum {
   read    = 1 << 2,  // 4
   write   = 1 << 1,  // 2
   execute = 1 << 0   // 1
} permission;  // A miniature version of UNIX file-permission masks

同样,移位操作全部来自C。

您现在可以编写:

permission all = read | write | execute;

您甚至可以将该行包括在权限声明本身中:

typedef enum {
   read    = 1 << 2,  // 4
   write   = 1 << 1,  // 2
   execute = 1 << 0,  // 1
   all     = read | write | execute // 7
} permission;  // Version 2

如何打开execute文件?

filePermission |= execute;

请注意,这很危险:

filePermission += execute;

这会将值更改all8,这没有任何意义。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

IntValue ?? 0 == -1?“-”:“ +”是什么意思?

MEth0 / 0/1是什么意思?

(x ^ 0x1)!= 0是什么意思?

0 < res <= (1 << 31) -1 - 这是什么意思?

&(0,1)在颠簸转换中是什么意思?

data[:,0] 或 data[:,1] 在熊猫中是什么意思

Golang中的0x1是什么意思?

这行代码是什么意思*((int *)(0))= 1 ;?

[-1:0] 在 Verilog 中是什么意思?

ghc的FUN_1_0,FUN_0_1等封闭类型是什么意思?

在clingo / ASP中1 <0和1 = -1是什么意思?

plt.plot(x[0:-1],y/y[0]) 是什么意思?

bugcheckcode 0x122的parameter1 0x4是什么意思?

如何表示像'1.108779411784206406864790428E-69'这样的数字,在Python中0-1之间

R mstate 包采用具有“状态”变量的数据。“状态”可以是 0 或 1。0 是什么意思,1 是什么意思?

rsync和xfr#1,to-chk = 0/1,它们是什么意思?

函数范围(len(sequence)-1,0,-1)中的数字是什么意思?

KPropertyX接口扩展了像:()-> V这样的表达式是什么意思

在“ls”的输出中,像“drwx--xr-x”这样的字符是什么意思?

是什么(“ {0,1}”,num);意思?

python中的transpose(3,0,1,2)是什么意思

布尔值“((number&1)== 0”)是什么意思?

Swift闭包中的$ 0和$ 1是什么意思?

批处理文件中%〜df0和%〜df1是什么意思?

hyperkube镜像标签末尾的0和1是什么意思

电池电量状态(0x2A1B)蓝牙规范是什么意思?

arr.sort(key = lambda x:(x [0],-x [1]))是什么意思?

SQL @x := @x +1 和 @x := 0 是什么意思?

<_io.BytesIO对象在0x000002244592F1A8>是什么意思?