Home Backend Development PHP Tutorial Introduction to the usage of php median operation

Introduction to the usage of php median operation

Jun 20, 2017 pm 01:32 PM
php Bit operations

phpBit operation is not commonly used in php, but its effect is quite large. Let’s introduce the usage of php bit operation.

  • $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 Shift)

Detailed explanation
$a & $b Bitwise AND sets the bits in $a and $b that are both 1 to 1;

例:10 & 12 = 8
Copy after login

10 1010
12 1100
1000 8
$a | $b Bitwise OR sets one of $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 notation sets the 0 in $a to 1 and the 1 to 0
Example:~10 =
10 1010 11111111111111111111111111111111111111111111111111111 110101 - 11
$a << $b left shift moves the value in $a to the left $b times (each move means multiplying by 2);
Example: 1 << 10 = 1024
1(1) Left shift 10 digits 10000000000(1024)
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 = 1256
10000000000(1024) Shifting 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 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 that are both 1 in $a and $b to 1; Example: 10 & 12 = 810 101012 1100 1000 8
$a | $b Bitwise OR sets the bits in $a or $b A value of 1 is set to 1; example: 10 | 12 = 1410 101012 1100 1110 14
$a ^ $b Bitwise XOR example: 10 ^ 1210 101012 1100 0110 6
~a Bitwise NOT The 0 in $a is set to 1, and the 1 is set to 0. Example: ~10 = 10 1010 11111111111111111111111111111111111111111111111111111110101 -11
$a << $b Move left to the Move left $b times (each move means multiplying by 2); example: 1 << 10 = 10241(1) Shifting left by 10 digits 10000000000(1024) is equivalent to 1*2 raised to the 10th power. There is no exponentiation operation in PHP It's really depressing.
$a > Shifting 2 bits is 100000000(256)


The combination of the flag field and the bit operator


in PHP Parameter value list for error_reporting

  • ##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 the following. 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 one is added to the power, one digit is added to the binary system (almost everyone who has studied computers knows:)...)
Note: Each option corresponds to one digit (1 is Turn on 0 to turn off)

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

Take three parameters as an example to see what the effect is

error_reporting(3);//decbin(3) == 0000 0011; (相当于设置 E_WARNING 和 E_ERROR )
error_reporting(4);//decbin(4) == 0000 0100;(相当于设置 E_PARSE )
error_reporting(5);//decbin(5) == 0000 0101;(相当于设置 E_PARSE 和 E_ERROR)
Copy after login
Get settings:

To determine whether a certain item is turned on, you can use bit operations to obtain it (& — The "AND" rule is all 1, otherwise it is 0)

//E_PARSE

if($n & 4){

//E_PARSE is turned on

//The binary of 4 is 0100, because only the third bit is 1, so when performing the "&" operation
All other
positions are set to 0
//So the result will only be when the third bit of $n is also 1 Greater than 0. //Such as 4(0100),5(0101),6(0110),7(0111)}else{
//E_PARSE is closed
//The third bit is 0 Indicates that this option is closed
}

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

In application, we may set the switch for a certain bit as needed.

See usage below.

//Close the E_PARSE item and use the ‘&’ “AND” rule
$n = $n&(8192-4-1);
//Why use 8191?
//This has something to do with the number of options you have. This error display mark uses a total of 13 bits (the binary number of 4096 is 13 bits), while 8192 is (14 bits).
//Why subtract 4 and subtract 1 Woolen cloth?
//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);

//To turn on the E_PARSE item, use ' |'"or" rule
$n = $n|4;
//After reading the above closing, you may have some ideas about opening:)
// '|' — "or" rule If there is 1, it is 1, otherwise it is 0
//The above is the case where all bits are 1, it does not affect other bits, now it becomes the case where 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:


The above is the detailed content of Introduction to the usage of php median operation. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles