In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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,
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.