什么是这个声明的含义是什么? [英] What is the meaning of this declaration?
问题描述
我不是在C / C ++的专家。
I am not a specialist in C/C++.
今天我发现了这个声明:
I found this declaration today:
typedef NS_OPTIONS(NSUInteger, PKRevealControllerType)
{
PKRevealControllerTypeNone = 0,
PKRevealControllerTypeLeft = 1 << 0,
PKRevealControllerTypeRight = 1 << 1,
PKRevealControllerTypeBoth = (PKRevealControllerTypeLeft | PKRevealControllerTypeRight)
};
你们可以翻译一下值每个值将有?
Can you guys translate what values every value will have?
推荐答案
算子的&LT;&LT;
终止的按位的左移操作符。移所有位向左的指定次数:(算术左移和储备符号位)
opertor <<
is bitwise left shift operator. Shift all the bits to left a specified number of times: (arithmetic left shift and reserves sign bit)
m << n
移 M
的所有位向左一个 N
的次数。 (的通知由两个单班==乘的)。
Shift all the bits of m
to left a n
number of times. (notice one shift == multiply by two).
1 LT;&LT; 0
手段的没有的转变所以它等于 1
而已。
1 << 0
means no shift so its equals to 1
only.
1 LT;&LT; 1
手段的有一个的转变所以它等于 1 * 2
= 2只。
1 << 1
means one shift so its equals to 1*2
= 2 only.
我跟一个字节解释:一个在一个字节是这样的:
I explain with one byte: one in one byte is like:
MSB
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 / 0
| / 1 << 1
| |
▼ ▼
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 2
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0
而 1 LT;&LT; 0
做什么,但它一样的人物之一。 (注意第7位被复制到preserve号)
Whereas 1 << 0
do nothing but its like figure one. (notice 7th bit is copied to preserve sign)
或接线员:逐位或
MSB PKRevealControllerTypeLeft
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | == 1
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0
| | | | | | | | OR
MSB PKRevealControllerTypeRight
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | == 2
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0
=
MSB PKRevealControllerTypeBoth
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | == 3
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0
|
是位聪明的运营商。下面code将其或
1 | 2
== 3
|
is bit wise operator. in below code it or
1 | 2
== 3
PKRevealControllerTypeNone = 0, // is Zero
PKRevealControllerTypeLeft = 1 << 0, // one
PKRevealControllerTypeRight = 1 << 1, // two
PKRevealControllerTypeBoth = (PKRevealControllerTypeLeft |
PKRevealControllerTypeRight) // three
有没有更多的技术理由来这样的初始化值,定义这样使事情排队很好地阅读这样的回答:的定义了一种(1 LT;℃下)
There is not more technical reason to initialized values like this, defining like that makes things line up nicely read this answer:define SOMETHING (1 << 0)
编译器优化其转换更简单的像:(我不知道对于第三个,但我认为编译器将优化太的)
compiler optimization convert them in simpler for like: (I am not sure for third one, but i think compiler will optimize that too)
PKRevealControllerTypeNone = 0, // is Zero
PKRevealControllerTypeLeft = 1, // one
PKRevealControllerTypeRight = 2, // two
PKRevealControllerTypeBoth = 3, // Three
编辑: @thanks耕种。
阅读这个答案<一个href=\"http://stackoverflow.com/questions/8956364/app-states-with-bool-flags?answertab=votes#8956606\">App与BOOL标志美国显示你有使用逐位运算符声明的有效性。
@thanks to Till. read this answer App States with BOOL flags show the usefulness of declarations you got using bit wise operators.
这篇关于什么是这个声明的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!