PHP Bitwise operators

Source: Internet
Author: User
Tags bitwise 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

  Positive number of the original code, anti-code, the complement is the same, negative numbers are not the same

Original code: 8-bit binary of the number, sign bit (first bit) 0 for positive, 1 for negative

Anti-code: Positive anti-code and the same as the original code, negative anti-code is the sign bit 1 unchanged, every bit of integer binary digit negation, get the inverse code

Complement: positive complement and the same as the original code, negative anti-code sign bit 1 unchanged, bitwise negation, the end (the lowest bit) plus 1; Computing in the computer is in the form of a complement of the operation, storage is also complementary

  The features of the complement:
1, a negative integer (or the original code) and its complement (or complement) added, and for the module.
2, the complement of an integer and then seek complement, equal to the integer itself.
3, complement positive zero and negative 0 means the same method.
  
& Operators
<? PHP $m = 1; $n = 2; $mn $m $n ; Echo $mn;

Operation result is 0

Explanation: Convert 1, 2 to binary (8-bit) 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.

Negative & Operations

With 2&-7=? Calculation as an example:

(1), the calculation of 2 of the complement: 2-> original code:00000010-> anti-code:00000010-> complement: 00000010

(2), calculation-7 of the complement: -7-> original code:10000111->:11111000-> complement: 11111001

(3), the calculation of the complement of 2&-7, and the original code---2&-7 complement:00000000-> back code:00000000-> The original code: 00000000

(4), have 2&-7 the original code to get 2&-7 value, so 2&-7 = 0

| operator
<? PHP $m = 1; $n = 2; $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, 1 is 1, all 0 is 0, then the result is 00000011, so the output is 3.

^ operator
<? PHP $m = 1; $n = 2; $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 $m = 2; $m 1 = ~$m; Echo $m 1;

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: (the sign bit of the inverse code is invariant, the bitwise negation, and the end plus 1)

1000 0000 0000 0000 0000 0000 0000 0011

So the output is-3.

<< operators
<? PHP $m = 3; $m 1=$m << 1; Echo $m 1;

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.

View article: Conversion between original code, inverse code, and complement and simple operation

PHP bit Operation detailed

PHP bit operators in the detailed

PHP Bitwise operators

Related Article

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.