PHP bit arithmetic

Source: Internet
Author: User
Tags bitwise
This is described in the bit Operations manual for PHP.

Example name result
$a & $b and (bitwise AND) will set the 1 bit in the $a and $b to 1.
$a | $b or (bitwise OR) sets the bit in the $a or the $b as 1 to 1.
$a ^ $b Xor (Bitwise XOR) will set the different bits in the $a and $b to 1.
~ $a not (bitwise non) sets the bit in the $a to 0 and vice versa.
$a << $b Shift left moves the bits in the $a $b times (each move represents "multiplied by 2").
$a >> $b Shift right shifts the bits in the $a to the right $b times (each move represents "divided by 2").

But I want to know what it's supposed to be, to know the results of this operation in advance.
For example
& 13
134 | 234
98 ^ 7
Wait, these.

Reply content:

This is described in the bit Operations manual for PHP.

Example name result
$a & $b and (bitwise AND) will set the 1 bit in the $a and $b to 1.
$a | $b or (bitwise OR) sets the bit in the $a or the $b as 1 to 1.
$a ^ $b Xor (Bitwise XOR) will set the different bits in the $a and $b to 1.
~ $a not (bitwise non) sets the bit in the $a to 0 and vice versa.
$a << $b Shift left moves the bits in the $a $b times (each move represents "multiplied by 2").
$a >> $b Shift right shifts the bits in the $a to the right $b times (each move represents "divided by 2").

But I want to know what it's supposed to be, to know the results of this operation in advance.
For example
& 13
134 | 234
98 ^ 7
Wait, these.

If I don't understand it correctly ... What you're asking is how to do bit arithmetic manually, right?

Bit arithmetic is the binary operation mode ... You want to do the decimal digits of the bitwise operation of the first to turn them into binary ...

How to manually convert into the system I will not repeat ... You can go and see for yourself ... The main word bit arithmetic ...

If we have two digits 123 and 321 ... After conversion to binary is 001111011 and 101000001 ...

We understand 按位与 the literal meaning ... ... Just follow each bit to do with this operation ... As follows...

   001111011 & 101000001-------------   001000001  = 65

That means 123 & 321 = 按位或 ... And all 按位异或 the same reason ... It's simple, I'm not writing ...

按位非... Purely for the current number of reverse ... For example, we manually calculate the ~123 process as follows ...

~ 000 ... 001111011---------------------   111 ... 110000100  = Numbers that appear to be large

I think you've noticed the problem ... Put a number 按位非 behind ... The front will produce a lot of 1 ...

Will cause this number to look very big ... Big to hard to calculate ...

But it's not actually ... Our operating system cannot store infinitely large integers ...

In fact, it can only store all the numbers from 0 to 2 of the n-th party ... n may be large but not infinitely large ...

But this has created a new problem ... is the operating system does not know negative numbers ... And the negative numbers are very common ...

So people agreed ... When a binary digit length is n and the highest level is 1 ... That means it's a negative number ...

That is, the operating system accepts a negative 2 n-1 to 2 of the n-th square-1 of this range of integers ... It's perfect.

Back to the question ... So the number that looks big is actually a negative. How much is the negative?

The algorithm is like this ... Do a bitwise non-operation after removing the highest bit ... And then minus a minus one ...

That is ~123 =-(~ (~123))-1 = 124 ... The principle is very complex, but it is very simple to show ...

Next 左移 and 右移 ... The same is the literal meaning ... such as 123 << 2 ...

     001111011   << 2---------------   00111101100  = 492

When you move left, the bit that is vacated is 0 ... When you move to the right, the more bits that go out are dropped directly ...

That means if all the numbers are moving left or right ... The final result must be 0 ...

It suddenly feels as if you are speaking basic knowledge to a child. Well ... Anyway, that's it.

  • 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.