“|=”是什么意思? C++ 中的操作是什么意思?

发布于 2024-08-29 13:29:14 字数 95 浏览 6 评论 0原文

我有以下代码,我不明白它的意思:

var1 |= var2>0 ? 1 : 2;

任何人都可以帮助我!

I have the following code and I can't understand what does it mean:

var1 |= var2>0 ? 1 : 2;

Anyone can help me please!

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

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

发布评论

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

评论(8

雪花飘飘的天空 2024-09-05 13:29:15

运算符 |= 表示按位 OR 运算符赋值

The operator |= means Assignment by bitwise OR operator

-残月青衣踏尘吟 2024-09-05 13:29:14
if (var2 > 0)
  var1 = var1 | 1;
else 
  var1 = var1 | 2;

它是按位或。

if (var2 > 0)
  var1 = var1 | 1;
else 
  var1 = var1 | 2;

It's bitwise-or.

半步萧音过轻尘 2024-09-05 13:29:14

所有a op= b 运算符都是a = a op b 的快捷方式。

然而,由于 C++ 允许单独重写 opop=,因此您依赖于自定义类型的每个实现者保持一致。

All the a op= b operators are a shortcut to a = a op b.

However since C++ allows op and op= to be overridden separately you rely on each implementer of custom types to be consistent.

最冷一天 2024-09-05 13:29:14

它的 按位 OR 赋值

v1 |= v2;

是缩写为:

v1 = v1 | v2;

Its the Assignment by bitwise OR

v1 |= v2;

is short for:

v1 = v1 | v2;
贩梦商人 2024-09-05 13:29:14

条件?如果 cond 为 true,则 x : y 返回 x,否则返回 y。阅读 三元运算符

a |= b 是 < 的简写代码>a = a | b 正在分配 a | ba

a | ba按位或 b。 (例如 2 | 3 = 3 和 1 | 2 = 3 )

cond ? x : y returns x if cond is true and y otherwise. Read Ternary Operator

a |= b is shorthand for a = a | b which is assigning a | b to a

a | b is bitwise OR of a and b. ( e.g. 2 | 3 = 3 and 1 | 2 = 3 )

只是在用心讲痛 2024-09-05 13:29:14

整数可以用二进制表示,因此每个数字(位、开关)都是 1(开)或 0(关):

00000000  ==  0
00000001  ==  1
00000010  ==  2
00000011  ==  3
00000100  ==  4
00001000  ==  8
00010000  ==  16

按位或通过“合并”两组位来组合两个数字:

First number:     00110000
Second number:    00000010
Result:           00110010

如果某个位为 1,则输入数字,则结果将为 1。

与按位 AND 进行比较,它会找到两组位的“重叠”:

First number:     00110100
Second number:    10011110
Result:           00010100

如果输入数字中的某个位均为 1,则结果将为 1。

如果数字在变量 a 和 b 中,您可以将按位 OR/AND 结果放入新变量 c:

unsigned int c = a | b; // OR

unsigned int c = a & b; // AND

通常需要将结果放入两个变量之一,即

unsigned int c = a | b; // OR
c = a; // copy

因此,作为简写,您可以这样做只需一步:

a |= b; // merge b directly into a

Integers can be represented in binary, so that each digit (bit, switch) is 1 (on) or 0 (off):

00000000  ==  0
00000001  ==  1
00000010  ==  2
00000011  ==  3
00000100  ==  4
00001000  ==  8
00010000  ==  16

Bitwise OR combines two numbers by "merging" the two sets of bits:

First number:     00110000
Second number:    00000010
Result:           00110010

If a bit is 1 in EITHER of the input numbers, then it will be 1 in the result.

Compare with bitwise AND, which finds the "overlap" of the two sets of bits:

First number:     00110100
Second number:    10011110
Result:           00010100

If a bit is 1 in BOTH of the input numbers, then it will be 1 in the result.

If the numbers are in variables a and b, you can place the the bitwise OR/AND results into a new variable c:

unsigned int c = a | b; // OR

unsigned int c = a & b; // AND

Often the result needs to be placed into one of the two variables, i.e.

unsigned int c = a | b; // OR
c = a; // copy

So as a shorthand, you can do this in a single step:

a |= b; // merge b directly into a
雪落纷纷 2024-09-05 13:29:14

正如其他人所说,它是 v1 = v1 | 的缩写。 v2;
您可能遇到的另一种用法是布尔值。
鉴于:

bool b = /*some value*/

而不是说:

if(a)
  b = true;

你可能会看到:

  b |= a;

As others have said it is short for v1 = v1 | v2;
Another usage you might come across is with booleans.
Given:

bool b = /*some value*/

Instead of saying:

if(a)
  b = true;

you might see:

  b |= a;
神妖 2024-09-05 13:29:14

正如我之前的其他人所提到的,这意味着您最终将通过按位或进行赋值。

按位或可以通过取左侧和右侧的位模式并将它们放在彼此的顶部来说明。

在每一列中:0 + 0 给出 0,1 + 0 给出 1,0 + 1 给出 1,1 + 1 给出 1。
在布尔值的上下文中: false OR false == false、true OR false == true、false OR true == true、true OR true == true。

以下是按位 OR 和结果位模式的示例:
var1(11) |= var2(14) -->变量1(15)

    1011 (11)
OR  1110 (14)  
=   1111 (15)

As other people before me have mentioned, it means you'll end up with assignments by bitwise OR.

Bitwise OR can be illustrated by taking the left-hand and right-hand side bit-patterns and put them on top of eachother.

In each column: 0 + 0 gives 0, 1 + 0 gives 1, 0 + 1 gives 1, 1 + 1 gives 1.
In the context of booleans: false OR false == false, true OR false == true, false OR true == true, true OR true == true.

Here's an example of bitwise OR and the resulting bit pattern:
var1(11) |= var2(14) --> var1(15)

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