[英]In C: what does var = x | y | z; mean?
So I've recently started working with TI's CC2650 device and am trying to learn how to program it by studying some of their sample applications. 因此,我最近开始使用TI的CC2650器件,正在尝试通过研究其一些示例应用程序来学习如何对其进行编程。 I see a lot of variables declared in this format and I have no clue what it means:
我看到很多以这种格式声明的变量,但我不知道这意味着什么:
var1 = x | y | z;
In the example above, var1
is of type uint8_t. 在上面的示例中,
var1
的类型为uint8_t。
|
is the binary bitwise or
operator. 是二进制按位
or
运算符。 For example: 0x00ff | 0xff00
例如:
0x00ff | 0xff00
0x00ff | 0xff00
is 0xffff
. 0x00ff | 0xff00
是0xffff
。
bitwise OR operator, so if you have x = 5 (101) y = 8 (1000) and z = 20 (10100), values in parenthesis are binary values so x | y | z = 101 | 1000 | 10100 = 11101
按位或运算符,因此,如果您有x = 5(101)y = 8(1000)和z = 20(10100),则括号中的值是二进制值,因此
x | y | z = 101 | 1000 | 10100 = 11101
x | y | z = 101 | 1000 | 10100 = 11101
The operator |
运营商
|
in C is known as bitwise OR operator . C中的in称为按位OR运算符 。 Similar to other bitwise operators (say AND
&
), bitwise OR only operates at the bit level. 与其他按位运算符(例如AND
&
)相似,按位OR只能在位级别上运行。 Its result is a 1
if one of the either bits is 1
and zero only when both bits are 0
. 如果两个位之一为
1
则结果为1
;只有两个位均为0
时,结果为零。 The |
|
which can be called a pipe! 可以称为管道! Look at the following:
请看以下内容:
bit a bit b a | b (a OR b)
0 0 0
0 1 1
1 0 1
1 1 1
In the expression, you mentioned: 在表达式中,您提到:
var1 = x | y | z | ...;
as there are many |
因为有很多
|
in a single statement, you have to know that, bitwise OR operator has Left-to-right Associativity means the operations are grouped from the left. 在单个语句中,您必须知道,按位OR运算符具有从左到右的关联性,这意味着操作从左开始分组。 Hence the above expression would be interpreted as:
因此,以上表达式将被解释为:
var1 = (x | y) | z | ...
=> var1 = ((x | y) | z) | ...
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.