Home > Article > Backend Development > Detailed explanation of PHP bit operators
Recommendation: "PHP Video Tutorial"
Bit Operator
The bit operator refers to the binary bit from the low bit Perform operations after aligning to the high bit.
Symbol | Function | Example | Personal understanding |
---|---|---|---|
& | bitwise AND | $m & $n |
All 1’s are 1, otherwise 0 |
| | Bitwise OR | $m | $n |
All 0s are 0, and 1 is 1 |
^ | Bitwise XOR | $m | $n |
is different from 1 , the same as 0 |
~ | bitwise inversion | ~$m |
|
Shift right | $m >> $n
|
<?php
$m = 1;
$n = 2;
$mn = $m & $n;
echo $mn;
The operation result is 0
Explanation: Convert 1 and 2 into binary respectively as
00000001
00000010
In the process of bitwise AND, all 1s are 1, The comparison result is 00000000, so the output is 0
|Operator<?php
$m = 1;
$n = 2;
$mn = $m | $n;
echo $mn;
The operation result is 3. Similarly, it is converted into the binary number
00000001# as above
##00000010In the process of bitwise OR, if 1 is 1 and all 0 is 0, the result is 00000011, so the output is 3^ operator
<?php $m = 1; $n = 2; $mn = $m ^ $n; echo $mn;The running result is 3. Similarly, it is converted into the above binary number0000000100000010In the process of bitwise OR , the difference is 1, the same is 0, so the result is 00000011, and 3 is output.
~Operator
<?php $m = 2; $m1 = ~$m; echo $m1;The operation result is -3, which is thought-provoking. Note: In computers, negative numbers are expressed in the complement form of their positive values. 1: The 32-bit original code of 2 is 0000 0000 0000 0000 0000 0000 0000 00102: The bitwise inversion is 1111 1111 1111 1111 1111 1111 1111 1101Since the first number is 1 and the sign bit is 1, it is a negative number. Therefore, the complement form of its positive value is expressed as: (the sign bit remains unchanged, bitwise inversion, and 1 is added at the end) 1000 0000 0000 0000 0000 0000 0000 0011So the output is -3
aadd5699ea49e0a4f2bfd9ad5921d7f6>Operator
Shift one position to the right, similar to the << operator, except that this is a right shift, which is not done here. Too much explanation.
For more programming-related knowledge, please visit:Programming Teaching
! !The above is the detailed content of Detailed explanation of PHP bit operators. For more information, please follow other related articles on the PHP Chinese website!