Example of a PHP operator (iv) "bitwise operator"

Source: Internet
Author: User
Tags arithmetic operators bit set bitwise operators php operator
We've explained PHP arithmetic operators, PHP string operators, assignment operators, and today we'll give you a detailed introduction to the PHP operator "bitwise operator"

Bit operators are not often used in PHP, but they are still very useful, and we will illustrate the use of bitwise operators.

Bitwise operators are operations that align bits from low to high, which allows for the evaluation and operation of the bits specified in the integer number.

The operators in PHP are shown in the following table

Operator Description Example
& Bitwise-AND $m & $n
| Bitwise OR $m | & $n
^ Bitwise XOR OR $m ^ $n
~ Bitwise non-or bitwise-Reversed $m ~ $n
<< Move left $m << $n
>> Move right $m >> $n

Here's an example of the operators in the table above

<?php$m=1;                1=0 00000001$n=2;                 2=00000010$mn= $m & $n, Echo $mn. " <br/> "; $mn = $m ^ $n; Echo $mn." <br/> "; $mn = $m | $n; echo $mn;? >

Code Run Result:

Example Explanation:

$m & $n : Both are 1 at 1, otherwise 0. That is, the $a and $b are all 1 of the bit set to 1, otherwise set to 0.

00000001← $m
& 00000010← $b

The comparison result is 00000000, so the output 0

$m ^ $n: in the bitwise OR process, the difference is 1, the same is 0.
00000001← $m

^ 00000010← $n

So the result is 00000011, and then the output is 3.

$m | $n: in the bitwise OR of the process, there are 1 1, all 0 is 0,

00000001← $m

| 00000010← $n

The result is 00000011, so the output 3

The above example says "bitwise OR", "bitwise OR", "bitwise XOR", and let's take a look at the three examples that follow

Bitwise non-or bitwise- counter Example , the code is as follows

<?php$m = 2; $m 1 = ~ $m; echo $m 1;? >

Run results

At this point we run the result of-3, which needs attention here.

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


Left shift Right Move code example

<?php$m = n;     12=00001100$n = 3;     3=00000011$mn= $m << $n, Echo $mn. " <br/> "; $mn = $m >> $n; echo $mn;?>

Operation Result:

Example Explanation:

$m << $n: Moves the bits in the $m to the left $n times (each move represents "multiplied by 2", or "multiplied by 2$b").
0000 1100← $m
<< 0110 0000 Shift left 3, vacated position with 0 supplement

0110 0000 = 96


$m >> $n: Moves the bits in the $m to the right $n times (each move represents "divided by 2", which is "multiplied by 2-$b").
0000 1100← $m
<< 0000 0001 Right Shift 3, extra position cut off

0000 0001 = 1


The above is the details of the "bitwise operator" in the PHP operation, if anything is not understood, Can pay attention to our topic.alibabacloud.com, in the above to leave a message, we will give you the first time to answer or in topic.alibabacloud.com search, may be able to find what you think. In the next section, we will detail the logical operators in PHP operations.

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.