Understanding the behavior of a single ampersand operator (&) on integers
I understand that the single ampersand operator is normally used for a 'bitwise AND' operation. However, can anyone help explain the interesting results you get when you use it for comparison between two numbers?
For example;
(6 & 2) = 2
(10 & 5) = 0
(20 & 25) = 16
(123 & 20) = 16
There seems to be no logical link between these results - am I missing something? Online documentation only seems to refer to the comparison of booleans or single bits.
Compare the binary representations of each of those.
110 & 010 = 010
1010 & 0101 = 0000
10100 & 11001 = 10000
1111011 & 0010100 = 0010000
In each case, a digit is 1 in the result only when it is 1 on both the left AND right side of the input.
你需要将你的数字转换为二进制表示,然后你会看到结果之间的链接,如6&2 = 2实际上是110&010 = 010等10&5是1010&0101 = 0000
The binary and operation is performed on the integers, represented in binary. For example
110 (6)
010 (2)
--------
010 (2)
链接地址: http://www.djcxy.com/p/9886.html
上一篇: 参考:比较PHP的打印和回显
下一篇: 了解整数上单个&符号运算符(&)的行为