PHP bit operators in the detailed

Source: Internet
Author: User
Tags bitwise operators

Bitwise operators

Bitwise operators are operations that align bits from low to high.

symbols function Example Personal Understanding
& Bitwise-AND $m & $n Full 1 is 1, otherwise 0
| Bitwise OR $m | $n Full 0 is 0, 1 is 1
^ Bitwise XOR OR $m | $n The difference is 1, the same is 0
~ Bitwise REVERSE ~$m
<< Shift left $m << $n
>> Shift Right $m >> $n
& Operators
<?php$m1;$n2;$mn$m$n;echo$mn;

Operation result is 0

Explanation: Convert 1, 2 to binary to

00000001

00000010

In the bitwise and process, the bitwise comparison, the total 1 is 1, the comparison result is 00000000, so the output 0

| operator
<?php$m1;$n2;$mn$m$n;echo$mn;

The result of the operation is 3, similarly, the conversion to the binary as above

00000001

00000010

In the bitwise OR of the process, there are 1 1, all 0 is 0, the result is 00000011, so the output 3

^ operator
<?php$m1;$n2;$mn$m$n;echo$mn;

The result of the operation is 3, similarly, the conversion to the binary as above

00000001

00000010

In the bitwise or process, the difference is 1, the same is 0, so the result is 00000011, and then output 3.

~ operator
<?php$m2;$m1 = ~$m;echo$m1;

The result of the operation is-3, here is a sobering.

Note: In a computer, negative numbers are expressed in the form of a positive complement.

1:2 of the 32-bit original code is 0000 0000 0000 0000 0000 0000 0000 0010

2: Bitwise reversed after 1111 1111 1111 1111 1111 1111 1111 1101

Since the first number is 1 and the sign bit is 1, it is negative, so the complement of its positive value is expressed as: (symbol bit invariant, bitwise negation, end plus 1)

1000 0000 0000 0000 0000 0000 0000 0011

So the output is-3

<< operators
<?php$m3;$m1=$m1;echo$m1;

The result of the operation is 6

The essence of the left shift operation is to shift the binary value of the corresponding data to the left bit by bit, and fill 0 in the vacated position, the highest bit overflow and discard.

3 of the 32-bit original code is, 0000 0000 0000 0000 0000 0000 0000 0011

Move left one: 0000 0000 0000 0000 0000 0000 0000 0110

So for 6

According to the manual description you can see that the bitwise operation can be seen to move one bit to the left, it is to implement multiply 2 operation. The operation speed of the displacement operations is much higher than that of multiplication. Therefore, when the multiplication operation of the data is processed, the displacement operation can be used to obtain a faster speed.
It is suggested that all the multiplication operations of 2 are converted to displacement operations, which can improve the efficiency of the program operation.

>> operators

Move right one bit, similar to the << operator, except that this is the right shift, not too much explanation here.

PHP bit operators in the 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.