Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the application of bit operation in PHP actual projects?

Shulou Source: shulou.com Published: 2022-06-02 03:19:20 10月02日 Update

This article introduces how the bit operation is used in the actual PHP project. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Let's review the basics, as well as the advanced skills of bit operations in PHP real-world projects.

I. Review of bit operation knowledge

In the PHP manual, there is a special introduction to bit operations and bit operators.

Https://www.php.net/manual/zh/language.operators.bitwise.php

1) bitwise and operator: &

The reason why it is called bit operation means that in the process of operation, we will binary the two sets of values that require bit operation, and then align the two sets of binary numbers from the low bit to the left. The bit here refers to the position of the binary number. Bitwise and operator means that each of the two sets of values corresponds to each other, and if the same value is 1, the result is 1, otherwise it is 0.

Look at the PHP example:

Echo 1 & 3; / / output: 1

Why output 1?

That's because:

The binary representation of 1 is: 01.

The binary representation of 3 is 11.

Then the operation flow after the alignment of these two sets of binary results is as follows:

0111murmur01

If the bit operation is performed from right to left, the result is 1, otherwise it is 0. The result, of course, is 01. And the conversion of 01 to decimal is 1. So, the output is 1.

Look at another set of examples:

Echo 2 & 10; / / output: 2

Let's take a look at the operation:

101010-0010

The conversion of 0010 to decimal is 2.

Be sure to remember to align to the left. Or low-order alignment mode.

If you don't know how to convert decimal to binary or binary to decimal, no. So, here are two ways to help you.

Echo decbin (10); / / decimal to binary. Echo bindec (10); / / binary to decimal. 2) Bitwise or operator: |

This is slightly different from the bitwise and operator above: as long as one of the two sets of numbers is 1, the result is 1.

Look at the example:

Echo 2 | 10; / / output result: 10

Operation process:

101010-1010

The decimal result of 1010 is 10.

3) bitwise XOR operator: ^

This operator is interesting: two sets of numbers must be a set of 0 and 1 for the result to be 1. 1 and 1 and 0 and 1 are both 0.

Look at the example:

Echo 1 ^ 1; / / output result: 0echo 1 ^ 0; / / output result: 1

1 ^ 1 operation process:

0101While 00

1 ^ 0 operation process:

0100,000,000,000,000,100

This is very simple. There's nothing to say. Anyway, the difference is 1, and the same is 0.

4) other bit operators: ~, > >, right shift operator,

Tags: Operations operators advertisements conditions results binary actual fields users decimal decimal login output that is knowledge examples procedures restrictions projects pictures Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Information Docker vpn MariaDB