A detailed explanation of the bit operations of PHP

Source: Internet
Author: User
PHP operators have a class of bit operations, this article and we share the PHP bit operation in detail, I hope to help everyone.

One:& and bitwise with

$a & $b will set the binary $ A and binary $b to 1 and the other bits to 0.1

For example:

7&3=>7 (0111) &3 (0011) can be seen, 0111 and 0011 are 1 of only the back 2 1, then 7 (0111) &3 (0011) = 0011; 110 binary is 3, then 7&3=3

One practical use of bitwise and is to judge parity $a &1

Principle Analysis:

1 binary only the last one is 1, the other bits are 0, and the last of the odd is 1,

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

This method determines the high performance of odd even ratio $a%2==1.

Two: | or bitwise-SAME OR

$a | $b will set $ A and any of the $b as 1 bits to 1.

Example Ibid.

Three: ^ XOR bitwise XOR

$a ^ $b will set a bit of $ A and $b to 1 for one of 1 and the other for 0.

Example Ibid.

FOUR: ~ Not bitwise reversed

$a ~ $b sets the bit in $ A to 1 and vice versa.

Example Ibid.

V: << Shift Left

$a << $b; moves the bits in $ A to the left $b times (each move represents "multiplied by 2").

Bit-based operations are faster than multiplication operations, so $a*2 can be written in $a<<1

Six: >>shift right (move to the left)

$a >> $b; moves the bits in $ A to the right $b times (each move represents "multiplied by 2").

Bit-based operations are faster than multiplication operations, so $A/2 can be written in $a>>1

Related recommendations:

Example of a PHP bitwise operator permission operation

Examples summarize the use of PHP bit operators

PHP bit operator Usage example detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.