The calculation results of the two are the same (for their respective calculation objects), but the performance is different.
& |: Logical operator
& |: Bitwise Operator
A and B are both true and true.
| Yes or, a | B is true if either of them is true.
&, | It is a bitwise operator, that is, bitwise operations,
Such as 00000011 & 00000001 = 00000001
00000011 | 00000001 = 00000011
For (&, |), the calculation object is a logical value, that is, true/false.
There are only four cases of calculation results.
True & True = true
True & false = false
False & True = false
False & false = false
True | true = true
True | false = true
False | true = true
False | false = false
For (&, |), the calculated object is a bit, that is, 1/0
There are only four cases of calculation results.
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0
& And & the results are the same for their respective calculation objects.
Similarly, | and | are the same.
For example, the calculation result of 5 & 2 is treated in this way.
First 5, non-zero, that is, true
2, non-zero, true
True & True = true
The result is true. The entire operation process requires transformation. Instead of directly using the binary comparison of operands. The results are different. During comparison, true should be converted to a binary value similar to 11111111, and false may be converted to 00000000.
The same is true here.
& The operation object is true/false. No matter what type of expression you use, it must first be converted to one of true/false before calculation.
& The calculation object is 0/1. No conversion is required. The number of BITs can be calculated directly.