Home Backend Development PHP Tutorial Detailed introduction to the usage of php bit operations_PHP tutorial

Detailed introduction to the usage of php bit operations_PHP tutorial

Jul 13, 2016 pm 05:14 PM
php under introduce Bit effect us yes usage detailed Operation

PHP bit operations are not commonly used in PHP, but they are very useful. Let’s introduce the usage of PHP bit operations.

$a & $b and(bitwise AND)
$a | $b or (bitwise OR)
$a ^ $b Xor (bitwise XOR)
~$a Not(bitwise not)
$a << $b Shift left(shift left)
$a >> $b Shift right(shift right)
Detailed explanation
$a & $b bitwise AND sets the bits in $a and $b that are both 1 to 1;
Example: 10 & 12 = 8
10 1010
12 1100
1000 8
$a | $b bitwise OR sets either $a or $b to 1;
Example: 10 | 12 = 14
10 1010
12 1100
1110 14
$a ^ $b bitwise XOR
Example: 10^12
10 1010
12 1100
0110 6
~a Bitwise NOT Set the 0 in $a to 1, and set the 1 to 0
Example: ~10 =
10 1010 111111111111111111111111111111111111111111111111111111111110101 -11
$a << $b left shift Move $a to the left $b times (each move means multiplying by 2);
Example: 1 << 10 = 1024
1(1) Shift left 10 bits 10000000000(1024)
It is equivalent to 1*2 raised to the 10th power. It is really depressing that there is no power operation in PHP.
$a >> $b right shift Move $a to the right $b times (each move means dividing by 2);
Example: 1024 << 2 = 1256
10000000000(1024) shifted right by 2 bits is 100000000(256)
PHP is the operation $a & $b and (bitwise AND) $a | $b or (bitwise OR) $a ^ $b Xor (bitwise exclusive OR) ~ $a Not (bitwise not) $a < < $b Shift left(shift left)$a >> $b Shift right(shift right)
Detailed explanation of $a & $b bitwise AND. Set the bits in $a and $b that are both 1 to 1; example: 10 & 12 = 810 101012 1100 1000 8
$a | $b bitwise OR sets one of $a or $b to 1; example: 10 | 12 = 1410 101012 1100 1110 14
$a ^ $b Bitwise XOR example: 10 ^ 1210 101012 1100 0110 6
~a bitwise notation sets the 0 in $a to 1 and the 1 to 0. Example: ~10 = 10 1010 111111111111111111111111111111111111111111111111111111111110101 -11
$a << $b left shift moves the bit in $a to the left $b times (each move means multiplying by 2); example: 1 << 10 = 10241(1) left shift 10 bits 10000000000 (1024) is equivalent to 1*2 raised to the 10th power. It is really depressing that there is no power operation in PHP.
$a >> $b Right shift moves the value in $a to the right $b times (each move means dividing by 2); example: 1024 << 2 = 125610000000000(1024) Right shift 2 bits That’s 100000000(256)


The combination of flag bit fields and bit operators


Parameter value list of error_reporting in PHP

value constant
1 E_ERROR
2 E_WARNING
4 E_PARSE
8 E_NOTICE
16 E_CORE_ERROR
32 E_CORE_WARNING
64 E_COMPILE_ERROR
128 E_COMPILE_WARNING
256 E_USER_ERROR
512 E_USER_WARNING
1024 E_USER_NOTICE
2047 E_ALL
2048 E_STRICT
4096 E_RECOVERABLE_ERROR

I found that the values ​​​​of value are all jumping, and they are all 2 to the n+1 power.

Look at this one again. Convert the value of value to binary.

value constant
0000 0001 E_ERROR
0000 0010 E_WARNING
0000 0100 E_PARSE
0000 1000 E_NOTICE
0001 0000 E_CORE_ERROR
0010 0000 E_CORE_WARNING
.
.
.
... ...Every time you add one power, you add one bit to the binary system (almost everyone who has studied computers knows this:)...)
Note: Each option corresponds to a bit (1 means on, 0 means off)

Okay, let’s take a look at the benefits of setting parameters like this.

Let’s take three parameters as an example to see the effect

error_reporting(3);//decbin(3) == 0000 0011; (equivalent to setting E_WARNING and E_ERROR)

error_reporting(4);//decbin(4) == 0000 0100; (equivalent to setting E_PARSE)

error_reporting(5);//decbin(5) == 0000 0101; (equivalent to setting E_PARSE and E_ERROR)

Get settings:

The judgment of whether an item is enabled can be obtained by using bit operations (& - "AND" rule: all 1s are 1, otherwise 0)

//E_PARSE
if($n & 4){
//E_PARSE is on
//The binary number of 4 is 0100, because only the third bit is 1, so when the "&" operation is performed, all other positions are set to 0
//So the result will be greater than 0 only when the third bit of $n is also 1.
//Such as 4(0100),5(0101),6(0110),7(0111)
}else{
//E_PARSE close
//The third bit is 0, which means this option is closed
}

Change settings: ($n represents the current decimal value)

During application, we may set the switch for a certain position as needed.
See usage below.

//Close the E_PARSE item and use the ‘&’ “AND” rule
$n = $n&(8192-4-1);
//Why use 8191?
//This is related to the number of your options. This error display mark uses a total of 13 bits (4096 is 13 bits in binary), while 8192 is (14 bits).
//Why subtract 4 and subtract 1?
//8192-4-1=8187. (1111111111011) The binary number is 13 digits, which is the same as the maximum number of digits we use. And the corresponding value in the third bit is 0.
//Use this number to perform a bitwise "AND" operation with any number between 1 and 4096. Except for the third bit, which will be set to 0, the values ​​of other bits will not change? "AND" rule :)
//Similarly, if you want to turn off E_WARNING
//$n = $n&(8192-2-1);

//Open the E_PARSE item using the ‘|’ “or” rule
$n = $n|4;
//After reading the closing above, you may have some ideas about opening it:)
// ‘|’ — “OR” rule: 1 is 1, otherwise 0
//The above is that when all bits are 1, it does not affect other bits. Now, when all bits are 0, it will not affect other bits:)
//So we only need to set the corresponding value of the binary bit of the subsequent operand to 1, and all other bits to 0 will be OK.
//Did you find it? It happens to be the decimal value corresponding to each of our setting items:)

That’s the idea. If you want to operate the setting values ​​​​on multiple bits at the same time, it depends on how your operands are set.

We can consider this method when developing in the future when we need to set multiple options with one parameter at the same time:)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629039.htmlTechArticlePHP bit operations are not commonly used in PHP, but they are quite useful. Let’s introduce the PHP bits Operation usage. $a $b and (bitwise AND) $a | $b or (bitwise OR) $a ^ $b Xor (bitwise exclusive OR)...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles