php位元運算在php中不常用到,但作用是相當大的,下面我們來介紹一下php位元運算用法。
$a & $b and(位元與)
$a | $b or(位元或)
$a ^ $b Xor(位元異或)
#~$a Not(位元非)
$a aa3f3e15d0032766d5f462fe0befd7a5> $b Shift right(右移)
詳解
$a & $b 位元與把$a和$b中都為1的位元設為1;
例:10 & 12 = 8
10 1010
12 1100
1000 8
$a | $b 按位或把$a或$b中有一個為1的為設為1;
例:10 | 12 = 14
10 1010
12 1100
1110 14
$a ^ $b 按位異或
例:10 ^ 12
10 1010
12 1100
0110 6
~a 按位非把$a中的為0的為設為1,1的為設為0
例:~10 =
10 1010 10101 - 11
$a fff5cb0a0f8d6974f777cc83015db033> $b 右移把$a中的為向右移動$b次(每一次移動都表示除以2);
例:1024 b66fb7b966e824789ba33a3fc08814aa> $b Shift right(右移)
詳解$a & $ b 按位與把$a和$b中都為1的位元設為1;例:10 & 12 = 810 101012 1100 1000 8
$a | $b 按位或把$a或$b中有一個為1的為設為1;例:10 | 12 = 1410 101012 1100 1110 14
$a ^ $b 按位異或例:10 ^ 1210 101012 1100 0110 6##~a $a中的為0的為設為1,1的為設為0例:~10 = 10 1010 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111122$移動$b次(每一次移動都表示乘以2);例:1 3db70be8586ea45b12ac6b3b42e727c7> $b 右移把$a中的為向右移動$b次(每一次移動都表示除以2);例:1024 << 2 = 125610000000000(1024) 右移2位元就是100000000(256)
#32 E_CORE_WARNING
64 E_COMPILE_ERROR
128 E_COMPILE_WARNING
128 E_COMPILE_WARNING
USER_ERROR
#1024 E_USER_NOTICE
#2047 E_ALL
#2048 E_STRICT
4096 E_RECOVERABLE_ERROR
發現value的值都是跳躍式的吧,而且全是2的n+1次方.再看下面這個。把value的值轉成二進位了。
value constant
0000 0001 E_ERROR
0000 0010 E_WARNING0000 0100 E_PARSE0000 1000 E_NOTICE
0001 0000$0000 1000 E_NOTICE
0001 0000 # #.
.
… …一次為每加一次方就是二進位加了一位(學過電腦的差不多都知道:)…)
注意:每個選項對應了一位(1為開啟0為關閉)
error_reporting(3);//decbin(3) == 0000 0011; (相当于设置 E_WARNING 和 E_ERROR ) error_reporting(4);//decbin(4) == 0000 0100;(相当于设置 E_PARSE ) error_reporting(5);//decbin(5) == 0000 0101;(相当于设置 E_PARSE 和 E_ERROR)
//Close the E_PARSE item and use the ‘&’ “AND” rule
$n = $n&(8192-4-1);
//Why use 8191?
//This has something to do with the number of options you have. This error display mark uses a total of 13 bits (the binary number of 4096 is 13 bits), while 8192 is (14 bits).
//Why subtract 4 and subtract 1 Woolen cloth?
//8192-4-1=8187. (1111111111011) The binary number is 13 digits, which is the same as the maximum number of digits we use. And the corresponding value in the third bit is 0.
//Use this number to perform a bitwise AND operation with any number between 1 and 4096. Except for the third bit, which will be set to 0, the values of other bits will not change? "AND" rule:)
//Similarly, if you want to turn off E_WARNING
//$n = $n&(8192-2-1);
//To turn on the E_PARSE item, use ' |'"or" rule
$n = $n|4;
//After reading the above closing, you may have some ideas about opening:)
// '|' — "or" rule If there is 1, it is 1, otherwise it is 0
//The above is the case where all bits are 1, it does not affect other bits, now it becomes the case where all bits are 0, it will not affect other bits:)
// So we only need to set the corresponding value of the binary bit of the subsequent operand to 1, and all other bits to 0 will be OK.
//Did you find it? It happens to be the decimal value corresponding to each of our setting items:
以上是php中位元運算的用法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!