|运算符与||操作员
简单的问题,但是 |
运算符相对于 ||
(或)运算符做什么?
Simple question but what does the |
operator do as opposed to the ||
(or) operator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
简单的问题,但是 |
运算符相对于 ||
(或)运算符做什么?
Simple question but what does the |
operator do as opposed to the ||
(or) operator?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
|
是按位OR
运算符,其中||
是逻辑OR
运算符。即,前者用于将两个数值中的位“组合”为并集,而如果运算符左侧或右侧的任一条件为真,则后者评估为真。具体来说,按位运算符(不要与逻辑运算符混淆)对数字的每一位(在相同的序数位置)进行运算,并相应地计算结果。在按位“OR”的情况下,如果任一位为 1,则结果位为 1;仅当两位都为 0 时,结果位为 0。例如,1|2 = 3,因为:
此外,2| 3 = 3,因为:
一开始这可能看起来令人困惑,但最终你会掌握它的窍门。按位
OR
主要用于在位字段上设置标志的情况。即,用单个值(通常是 32 位数字)保存一组相关条件的开/关状态的值。在 Win32 中,窗口样式值是位字段的一个很好的示例,其中每个样式都由单个位(或标志)表示,例如 WS_CAPTION,它指示窗口是否有标题栏。|
is a bitwiseOR
operator, where as||
is a logicalOR
operator. Namely, the former is used to "combine" the bits from two numeric values as a union, whereas the latter evaluates to true if either condition on the left or right side of the operator is true.Specifically, bitwise operators (not to be confused with logical operators) operate on each bit of the numbers (at the same ordinal position), and calculates a result accordingly. In the case of a bitwise
OR
, the resulting bit is 1 if either bit is 1, and 0 only if both bits are 0. For example, 1|2 = 3, because:Furthermore, 2|3 = 3, because:
This can seem confusing at first, but eventually you get the hang of it. Bitwise
OR
is used mostly in cases to set flags on a bit field. That is, a value holding the on/off state for a set of related conditions in a single value (usually a 32 bit number). In Win32, the window style value is a good example of a bit field, where each style is represented by a single bit (or flag), like WS_CAPTION, which indicates whether or not the window has a title bar.一个字有几个(通常是 32、16、8 或 64)位。按位 OR(一个竖线)返回该位位置中每个位位置的逻辑 OR。逻辑或(两个竖线)仅返回 TRUE 或 FALSE。
There are several (usually 32, 16, 8 or 64) bits in a word. The bitwise OR (one vertical bar) returns the logical OR for each bit postion in that bit position. The logical OR (two vertical bars) only returns TRUE or FALSE.
|
是按位或 运算符。维基百科页面 C 和 C++ 中的运算符很好地描述了所有运算符。|
is the bitwise or operator. The wikipedia page Operators in C and C++ describes all of the operators pretty well.正如其他人提到的,
|
是按位 OR 运算符,||
是逻辑 OR 运算符,并且它们概念上是不同的操作,(通常)对不同类型的输入进行操作。但这可能会引发另一个问题:如果您将|
与布尔操作数一起使用,那么这是否不会与||
做同样的事情,因为无论如何一切最终都归结为位?是否需要使用不同的||
运算符?除了概念上的差异之外,另一个重要的差异是
||
是短路。这意味着如果第一个操作数为 true,则根本不会计算第二个操作数。例如:仅当
Foo()
返回 0 时才会调用Bar()
。如果使用|
,则始终会计算两个操作数。(当然,
&
和&&
具有类似的行为。)As others have mentioned,
|
is the bitwise OR operator and||
is the logical OR operator, and they are conceptually different operations that (usually) operate on different sorts of inputs. But this then might raise another question: if you used|
with boolean operands, wouldn't that do the same thing as||
since everything ultimately boils down to bits anyway? Is a distinct||
operator necessary?Aside from the conceptual difference, another important difference is that
||
is short-circuiting. This means that if the first operand is true, the second operand isn't evaluated at all. For example:would invoke
Bar()
only ifFoo()
returns 0. If|
were used, both operands always would be evaluated.(And, of course,
&
and&&
have analogous behaviors.)||是一个逻辑或并且|是按位或。大多数时候,当您检查 if (i == 0 || i ==1) 等内容时,您只想使用 ||但是当您执行诸如将标志作为变量传递之类的操作时,请使用 |。 (如果您不知道那是什么,您可能根本不需要|)
The || is a logical or and the | is a bitwise or. Most of the time when you are checking things like if (i == 0 || i ==1) you just want to use || but when you are doing things like passing flags as a variable use |. (If you don't know what that is you probably don't need | at all)