Home  >  Article  >  Backend Development  >  Detailed explanation of PHP bit operators

Detailed explanation of PHP bit operators

藏色散人
藏色散人forward
2021-01-05 16:12:415014browse

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.

##887a7712dda16e147f6d179d5c9c66e0>Shift right
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
$m >> $n
##&operator

<?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

##00000010

In 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 number

00000001

00000010

In 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 0010

2: The bitwise inversion is 1111 1111 1111 1111 1111 1111 1111 1101

Since 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 0011

So 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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete