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

Detailed explanation of PHP bit operators

藏色散人
Release: 2023-04-09 17:30:01
forward
5063 people have browsed it

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.

##<<Shift left>>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
$m >> $n
##&operator

<?php
$m = 1;
$n = 2;
$mn = $m & $n;
echo $mn;
Copy after login
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;
Copy after login
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;
Copy after login
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;
Copy after login
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

<

<?php
$m = 3;
$m1=$m << 1;
echo $m1;
Copy after login
The result of the operation is 6

The essence of the left shift operation is to shift the binary value of the corresponding data to the left bit by bit, and fill in the vacated position with 0. The highest bit overflows and is discarded. The 32-bit original code of

3 is, 0000 0000 0000 0000 0000 0000 0000 0011

Shift one position to the left: 0000 0000 0000 0000 0000 0000 0000 0110

, so it is 6

According to the description in the manual, we can see that bitwise operations can be seen by shifting one bit to the left, which is a multiplication by 2 operation. Because the operation speed of displacement operation is much higher than that of multiplication. Therefore, when processing data multiplication operations, using displacement operations can achieve faster speeds.

Tip: Convert all multiplication operations of 2 into displacement operations to improve the running efficiency of the program.


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

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template