Detailed explanation of bit operations in php

小云云
Release: 2023-03-21 14:34:01
Original
2193 people have browsed it

There is a type of operators in php that are bit operations. This article mainly shares with you the detailed explanation of bit operations in php. I hope it can help you.

一:& And bitwise AND

$a&$b will set the binary $a and binary $b bits that are both 1 to 1, and the other bits to 0

For example:

7&3=>7(0111)&3(0011) It can be seen that 0111 and 0011 are both 1, only the last two 1s are, then 7(0111)&3(0011)=0011 ; 0011 is 3 in decimal, then 7&3=3

A practical use of bitwise AND is to determine parity $a&1

Principle analysis: The binary number of

1 only has the last digit is 1, other bits are all 0, and the last digit of an odd number is also 1,

For example, 11&1=>11(1011)&1(0001) = 1 is an odd number, 12&1=>12 (1100)&1=0 is an even number

This method has higher performance than $a%2==1 to determine odd and even numbers

Two:| Or bitwise or

$a|$b will set any bit in $a and $b to 1.

Example is the same as above

Three:^ Set to 1.

Example is the same as above

Four:~ Not bitwise inversion

$a~$b Set the bit that is 0 in $a to 1, and vice versa.

Example same as above

5: << Shift left (shift left)

$a<<$b;Move the bit in $a to the left$ b times (each move means "multiply by 2").

Bitwise operations are faster than multiplication and division operations, so $a*2 can be written as $a<<1

6: >>Shift right (shift right)

$a>>$b;Move the bit in $a to the right $b times (each move means "multiply by 2").

Bitwise operations are faster than multiplication and division operations, so $a/2 can be written as $a>>1

Related recommendations:

php bitwise operators Detailed examples of permission operations

Examples summarize the usage skills of php bit operators

Detailed examples of php bit operator usage

The above is the detailed content of Detailed explanation of bit operations in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!