多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
位运算符允许对整型数中指定的位进行求值和操作。 * `$a & $b`,And(按位与),将把a 和a和b 中都为 1 的位设为 1。 * `$a | $b`,Or(按位或),将把a 和a和b 中任何一个为 1 的位设为 1。 * `$a ^ $b`,Xor(按位异或),将把a 和a和b 中一个为 1 另一个为 0 的位设为 1。 * `~$a`,Not(按位取反),将$a 中为 0 的位设为 1,反之亦然。 * `$a << $b`,Shift left(左移),将a 中的位向左移动a中的位向左移动b 次(每一次移动都表示乘以 2)。 * `$a >> $b`,Shift right(右移),将a 中的位向右移动a中的位向右移动b 次(每一次移动都表示除以 2)。 **Example 1 整数的 AND,OR 和 XOR 位运算符。** ~~~php <?php /* * Ignore the top section, * it is just formatting to make output clearer. */ $format = '(%1$2d = %1$04b) = (%2$2d = %2$04b)' . ' %3$s (%4$2d = %4$04b)' . "\n"; echo <<<EOH --------- --------- -- --------- result value op test --------- --------- -- --------- EOH; /* * Here are the examples. */ $values = array(0, 1, 2, 4, 8); $test = 1 + 4; echo "\n Bitwise AND \n"; foreach ($values as $value) { $result = $value & $test; printf($format, $result, $value, '&', $test); } echo "\n Bitwise Inclusive OR \n"; foreach ($values as $value) { $result = $value | $test; printf($format, $result, $value, '|', $test); } echo "\n Bitwise Exclusive OR (XOR) \n"; foreach ($values as $value) { $result = $value ^ $test; printf($format, $result, $value, '^', $test); } ?> ~~~ 以上例程会输出: ~~~txt --------- --------- -- --------- result value op test --------- --------- -- --------- Bitwise AND ( 0 = 0000) = ( 0 = 0000) & ( 5 = 0101) ( 1 = 0001) = ( 1 = 0001) & ( 5 = 0101) ( 0 = 0000) = ( 2 = 0010) & ( 5 = 0101) ( 4 = 0100) = ( 4 = 0100) & ( 5 = 0101) ( 0 = 0000) = ( 8 = 1000) & ( 5 = 0101) Bitwise Inclusive OR ( 5 = 0101) = ( 0 = 0000) | ( 5 = 0101) ( 5 = 0101) = ( 1 = 0001) | ( 5 = 0101) ( 7 = 0111) = ( 2 = 0010) | ( 5 = 0101) ( 5 = 0101) = ( 4 = 0100) | ( 5 = 0101) (13 = 1101) = ( 8 = 1000) | ( 5 = 0101) Bitwise Exclusive OR (XOR) ( 5 = 0101) = ( 0 = 0000) ^ ( 5 = 0101) ( 4 = 0100) = ( 1 = 0001) ^ ( 5 = 0101) ( 7 = 0111) = ( 2 = 0010) ^ ( 5 = 0101) ( 1 = 0001) = ( 4 = 0100) ^ ( 5 = 0101) (13 = 1101) = ( 8 = 1000) ^ ( 5 = 0101) ~~~ [](javascript:;) 下一步