位操作符
位操作符用于对操作数执行位级操作。首先将运算符转换为位级,然后对操作数进行计算。数学运算,如加法、减法、乘法等,可以在位级进行,处理速度更快。在PHP中,在位级工作的运算符是:
- &(位和):这是一个二进制运算符,也就是说,它在两个操作数上工作。在PHP中,位和运算符接受两个数字作为操作数,并对两个数字的每一个位执行操作。只有当两个比特都是1时,AND的结果才为1。
语法
1234$First & $SecondThis will return another number whose bits areset if both the bit of first and second are set.
例子
12345678Input: $First = 5, $Second = 3Output: The bitwise & of both these value will be 1.Explanation:Binary representation of 5 is 0101 and 3 is 0011.Therefore their bitwise & will be 0001 (i.e. setif both first and second have their bit set.)
- |(按位或):这也是二进制运算符,即工作在两个操作数。位操作符将两个数字作为操作数,并对两个数字的每一个位执行或操作。OR的结果是1两个比特中的任何一个都是1。
语法
1234$First | $SecondThis will return another number whose bits areset if either the bit of first or second are set.
例子
12345678Input: First = 5, Second = 3Output: The bitwise | of both these value will be 7.Explanation:Binary representation of 5 is 0101 and 3 is 0011.Therefore their bitwise | will be 0111 (i.e. setif either first or second have their bit set.)
- ^(位XOR):这也是二元运算符即作用于两个操作数。这也称为独占或操作符。按位XOR将两个数字作为操作数,并对两个数字的每一位执行XOR操作。如果两个比特不同,XOR的结果是1。
语法
12345$First ^ $SecondThis will return another number whose bits areset if one of the bit in first or second isset but not both.
例子
123456789Input: First = 5, Second = 3Output: The bitwise ^ of both these value will be 6.Explanation:Binary representation of 5 is 0101 and 3 is 0011.Therefore their bitwise ^ will be 0110 (i.e. setif either first or second have their bit set butnot both.)
- ~(位不):这是一个一元运算符,也就是说只对一个操作数工作。位非运算符取一个数字并对它的所有位进行逆运算。
语法
123~$numberThis will invert all the bits of $number.
例子
12345678Input: number = 5Output: The bitwise '~' of this number will be -6.Explanation:Binary representation of 5 is 0101. Therefore thebitwise ~ of this will be 1010 (inverts all thebits of the input number) - <<(按位左移):这是一个二进制运算符,也就是对两个操作数进行操作。位左移位运算符取两个数字,左移位第一个操作数的位,第二个操作数决定移位的位置数。
语法
12345$First << $SecondThis will shift the bits of $First towards theleft. $Second decides the number of time thebits will be shifted.
例子
12345678Input: First = 5, Second = 1Output: The bitwise << of both these value will be 10.Explanation:Binary representation of 5 is 0101 . Therefore,bitwise << will shift the bits of 5 one timestowards the left (i.e. 01010 )
注意:按位左移1位等于乘以2。 - >>(按位右移):这也是一个二进制运算符,也就是对两个操作数进行操作。位向右移位运算符取两个数字,右移位第一个操作数的位,第二个操作数决定移位的位置数。
语法
12345$First >> $SecondThis will shift the bits of $First towards theright. $Second decides the number of time thebits will be shifted.
例子
12345678Input: First = 5, Second = 1Output: The bitwise >> of both these value will be 2.Explanation:Binary representation of 5 is 0101 . Therefore,bitwise >> will shift the bits of 5 one timestowards the right(i.e. 010)
注:按位右移1位等于按2除。
下面是PHP中逐位运算符的实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?php // PHP code to demonstrate Bitwise Operator. // Bitwise AND $First = 5; $second = 3; $answer = $First & $second; print_r("Bitwise & of 5 and 3 is $answer"); print_r("\n"); // Bitwise OR $answer = $First | $second; print_r("Bitwise | of 5 and 3 is $answer"); print_r("\n"); // Bitwise XOR $answer = $First ^ $second; print_r("Bitwise ^ of 5 and 3 is $answer"); print_r("\n"); // Bitwise NOT $answer = ~$First; print_r("Bitwise ~ of 5 is $answer"); print_r("\n"); // Bitwise Left shift $second = 1; $answer = $First << $second; print_r("5 << 1 will be $answer"); print_r("\n"); // Bitwise Right shift $answer = $First >> $second; print_r("5 >> 1 will be $answer"); print_r("\n"); ?> |
输出
1 2 3 4 5 6 | Bitwise & of 5 and 3 is 1 Bitwise | of 5 and 3 is 7 Bitwise ^ of 5 and 3 is 6 Bitwise ~ of 5 is -6 5 << 1 will be 10 5 >> 1 will be 2 |