& 和 | 是做什么的运营商呢?它们与 && 和 || 有何不同? Swift
What do the & and | operators do? How are they different from && and ||? Swift
我见过很多使用 |
或 &
的实例,但我不明白它们的用途。我知道 &&
和 ||
是什么意思,但我想知道它们和对应的单字符之间的区别。此外,如果有人可以向我展示使用 |
和 &
的示例(请在 Swift 2 中),那就太好了。谢谢
您可以在 Swift 文档中了解它们及其差异。
Logical Operators 1
Logical operators modify or combine the Boolean logic values true and
false. Swift supports the three standard logical operators found in
C-based languages:
Logical NOT (!a)
Logical AND (a && b)
Logical OR (a || b)
Bitwise Operators 2
Bitwise operators enable you to manipulate the individual raw data
bits within a data structure. They are often used in low-level
programming, such as graphics programming and device driver creation.
Bitwise operators can also be useful when you work with raw data from
external sources, such as encoding and decoding data for communication
over a custom protocol.
Swift supports all of the bitwise operators found in C, as described
below.
请参阅 Wikipedia page on Bitwise Operation and the Swift documentation 了解按位运算符。
这些是按位运算符。 &
是按位与,|
是按位或。
查看这些示例:
0011 (decimal 3)
AND 0010 (decimal 2)
= 0010 (decimal 2)
0101 (decimal 5)
OR 0011 (decimal 3)
= 0111 (decimal 7)
Source: Wikipedia
之前在 Whosebug 上讨论过按位运算符的使用:
- practical applications of bitwise operations
- Using bitwise operations
按位异或的使用(不是你的问题,但无论如何是一个很酷的逻辑门)引起了我的注意(来自@Vilx- here)(我不知道如何它有效,但答案被接受并投票 34 次)
编辑: 如果您想知道它是如何工作的,XOR swap algorithm - Wikipedia
上有一个很好的简单证明
Swapping two integer variables without an intermediary variable:
A = A^B // A is now XOR of A and B
B = A^B // B is now the original A
A = A^B // A is now the original B
如果这些没有帮助,我已经在这个 post 中链接到两次的维基百科页面有一个 Applications 部分,但它们并不真正适用于高级语言(除非出于某种原因,您想优化算法以仅使用按位运算。
&
和 |
是按位运算符,它们比较位表示(二进制值)。
`&` -bitwise AND operator
`|` -bitwise OR operator
&&
和 ||
用于比较 int,double,etc.
等基元情况下的实际值,而 objects.They 情况下的地址被称为逻辑运算符。
1. Lets go to an example to make it simpler (bitwise operator):
int p=9; int q=10;
//binary representation of 9 and 10 are 1001 and 1010 respectively.
现在,p & q
returns 在每个位位置一个 1
两个操作数对应的位都是1.
p|q
returns 一个或两个操作数的相应位为 1 的每个位位置中的一个。
2.An example for logical operators.
int p=9; int q=10;
p>0 && q>0
returns true 因为 p
和 q
都大于 0
(因为两边的条件运算符为真)。
p>0 || q<0
returns 为真,因为至少有一个条件为真(这里,p 大于 0)。
我希望这能帮助您理解 bitwise
和 logical
运算符之间的区别。
我见过很多使用 |
或 &
的实例,但我不明白它们的用途。我知道 &&
和 ||
是什么意思,但我想知道它们和对应的单字符之间的区别。此外,如果有人可以向我展示使用 |
和 &
的示例(请在 Swift 2 中),那就太好了。谢谢
您可以在 Swift 文档中了解它们及其差异。
Logical Operators 1
Logical operators modify or combine the Boolean logic values true and false. Swift supports the three standard logical operators found in C-based languages:
Logical NOT (!a) Logical AND (a && b) Logical OR (a || b)
Bitwise Operators 2
Bitwise operators enable you to manipulate the individual raw data bits within a data structure. They are often used in low-level programming, such as graphics programming and device driver creation. Bitwise operators can also be useful when you work with raw data from external sources, such as encoding and decoding data for communication over a custom protocol.
Swift supports all of the bitwise operators found in C, as described below.
请参阅 Wikipedia page on Bitwise Operation and the Swift documentation 了解按位运算符。
这些是按位运算符。 &
是按位与,|
是按位或。
查看这些示例:
0011 (decimal 3)
AND 0010 (decimal 2)
= 0010 (decimal 2)
0101 (decimal 5)
OR 0011 (decimal 3)
= 0111 (decimal 7)
Source: Wikipedia
之前在 Whosebug 上讨论过按位运算符的使用:
- practical applications of bitwise operations
- Using bitwise operations
按位异或的使用(不是你的问题,但无论如何是一个很酷的逻辑门)引起了我的注意(来自@Vilx- here)(我不知道如何它有效,但答案被接受并投票 34 次)
编辑: 如果您想知道它是如何工作的,XOR swap algorithm - Wikipedia
上有一个很好的简单证明Swapping two integer variables without an intermediary variable:
A = A^B // A is now XOR of A and B
B = A^B // B is now the original A
A = A^B // A is now the original B
如果这些没有帮助,我已经在这个 post 中链接到两次的维基百科页面有一个 Applications 部分,但它们并不真正适用于高级语言(除非出于某种原因,您想优化算法以仅使用按位运算。
&
和 |
是按位运算符,它们比较位表示(二进制值)。
`&` -bitwise AND operator
`|` -bitwise OR operator
&&
和 ||
用于比较 int,double,etc.
等基元情况下的实际值,而 objects.They 情况下的地址被称为逻辑运算符。
1. Lets go to an example to make it simpler (bitwise operator):
int p=9; int q=10;
//binary representation of 9 and 10 are 1001 and 1010 respectively.
现在,
p & q
returns 在每个位位置一个 1 两个操作数对应的位都是1.p|q
returns 一个或两个操作数的相应位为 1 的每个位位置中的一个。2.An example for logical operators. int p=9; int q=10;
p>0 && q>0
returns true 因为p
和q
都大于0
(因为两边的条件运算符为真)。p>0 || q<0
returns 为真,因为至少有一个条件为真(这里,p 大于 0)。
我希望这能帮助您理解 bitwise
和 logical
运算符之间的区别。