|运算符与||操作员

发布于 2024-09-17 17:47:46 字数 64 浏览 9 评论 0原文

简单的问题,但是 | 运算符相对于 || (或)运算符做什么?

Simple question but what does the | operator do as opposed to the || (or) operator?

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

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

发布评论

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

评论(5

轻许诺言 2024-09-24 17:47:46

| 是按位 OR 运算符,其中 || 是逻辑 OR 运算符。即,前者用于将两个数值中的位“组合”为并集,而如果运算符左侧或右侧的任一条件为真,则后者评估为真。

具体来说,按位运算符(不要与逻辑运算符混淆)对数字的每一位(在相同的序数位置)进行运算,并相应地计算结果。在按位“OR”的情况下,如果任一位为 1,则结果位为 1;仅当两位都为 0 时,结果位为 0。例如,1|2 = 3,因为:

1 = 0001
2 = 0010
--------
    0011 = 3

此外,2| 3 = 3,因为:

2 = 0010
3 = 0011
--------
    0011 = 3

一开始这可能看起来令人困惑,但最终你会掌握它的窍门。按位OR 主要用于在位字段上设置标志的情况。即,用单个值(通常是 32 位数字)保存一组相关条件的开/关状态的值。在 Win32 中,窗口样式值是位字段的一个很好的示例,其中每个样式都由单个位(或标志)表示,例如 WS_CAPTION,它指示窗口是否有标题栏。

| is a bitwise OR operator, where as || is a logical OR 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:

1 = 0001
2 = 0010
--------
    0011 = 3

Furthermore, 2|3 = 3, because:

2 = 0010
3 = 0011
--------
    0011 = 3

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.

第几種人 2024-09-24 17:47:46

一个字有几个(通常是 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.

扮仙女 2024-09-24 17:47:46

|按位或 运算符。维基百科页面 C 和 C++ 中的运算符很好地描述了所有运算符。

| is the bitwise or operator. The wikipedia page Operators in C and C++ describes all of the operators pretty well.

意中人 2024-09-24 17:47:46

正如其他人提到的,|按位 OR 运算符,||逻辑 OR 运算符,并且它们概念上是不同的操作,(通常)对不同类型的输入进行操作。但这可能会引发另一个问题:如果您将 | 与布尔操作数一起使用,那么这是否不会与 || 做同样的事情,因为无论如何一切最终都归结为位?是否需要使用不同的 || 运算符?

除了概念上的差异之外,另一个重要的差异是 ||短路。这意味着如果第一个操作数为 true,则根本不会计算第二个操作数。例如:

int flag = Foo() || Bar();

仅当 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:

int flag = Foo() || Bar();

would invoke Bar() only if Foo() returns 0. If | were used, both operands always would be evaluated.

(And, of course, & and && have analogous behaviors.)

夏の忆 2024-09-24 17:47:46

||是一个逻辑或并且|是按位或。大多数时候,当您检查 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)

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