PHP bit operator detailed ____php

Source: Internet
Author: User
Bitwise Operators

Bitwise operators are operations that are aligned with bits from low to high.

symbols function examples Personal Understanding
& Bitwise AND $m & $n All 1 to 1, otherwise 0.
| by bit or $m | $n All 0 is 0, 1 is 1.
^ Per-bitwise XOR OR $m | $n The difference is 1, the same is 0
~ Reverse by position ~ $m
<< Shift left $m << $n
>> Shift Right $m >> $n
& operator
<?php
$m = 1;
$n = 2;
$mn = $m & $n;
Echo $mn;

Run result is 0

Explanation: Converts 1, 2, respectively, to binary

00000001

00000010

In bitwise-by-bit comparison, all 1 is 1, the result is 00000000, so the output 0 | operator

<?php
$m = 1;
$n = 2;
$mn = $m | $n;
Echo $mn;

The run result is 3, likewise, converted into binary

00000001

00000010

In the bitwise or process, 1 is 1, all 0 is 0, then the result is 00000011, so the 3 ^ operator is output

<?php
$m = 1;
$n = 2;
$mn = $m ^ $n;
Echo $mn;

The run result is 3, likewise, converted into binary

00000001

00000010

In the bitwise also or the 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 is 3, which is thought-provoking here.

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

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

2:1111 1111 1111 1111 1111 1111 1111 1101 by bitwise reverse

Because the first number is 1, the sign bit is 1, which is negative, so the complement form with its positive value is expressed as follows: (symbol bit unchanged, bitwise reverse, end plus 1)

1000 0000 0000 0000 0000 0000 0000 0011

So the output is-3 << operator

<?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 move the binary value of the corresponding data to the left several bits at a time, and fill in the vacated position 0, the highest bit overflow and discard.

3 of 32-digit 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 to the left one bit, is to implement multiply 2 operation. Because the operation speed of displacement is much higher than the operation speed of multiplication. Therefore, when processing the multiplication of data, 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 running efficiency of the program. >> operator

One bit to the right, and the << operator, just like this one is right, not too much to explain here.

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.