PHP bit operator _php Tutorial

Source: Internet
Author: User
This involves the bottom of the system, Web development is almost useless, know that the next thing is good. The underlying interpretation of things is always simple.
Variables are stored in memory as binary digits. Integers are 4 bytes on 32-bit systems, one byte is 8 bits, or 32 bits, and bitwise operations are operations of bit-level operation.


$a << $b Shift left shifts the bits in the $a to the left $b times (each move represents "multiplied by 2").
Assumed variable $a The value is 3, $b The value is 2.
So $a The in-memory storage format is:
00000000 00000000 00000000 00000011
Each time the left shift is performed, all bits are moved to the left, the right side is 0, and the left exceeds the discard.
So $a Move left 2 What you get is:
00000000 00000000 00000000 00011000
That is 12. Equivalent 3x2x2 = 12.

$a >> $b Shift right shifts the bits in the $a to the right $b times (each move represents "divided by 2").
Assumed variable $a The value is , $b The value is 2.
So $a The in-memory storage format is:
00000000 00000000 00000000 00011000
Each time the right shift is performed, all the bits are moved, the right exceeds the discard, and the left empty bits complement 0.
So $a Move right 2 What you get is:
00000000 00000000 00000000 00000110
That is 6. Equivalent 24÷2÷2 = 6.

The displacement operation is the system bottom operation, the operation speed is much faster than the multiplication, the optimization code optimizes to the extreme time, may convert the number to two common multiple, then carries on the displacement computation. However, there is almost no need in web development, and beyond the bottom of the system, the portability of the program will be reduced.

The rest of the bitwise operators are similar. Like what:
$a & $b and (bitwise AND) will set the 1 bit in the $a and $b to 1.
Assumed variable $a The value is 5, $b The value is 6.
$a:
00000000 00000000 00000000 00000101
$b:
00000000 00000000 00000000 00000110
$a & $b :
00000000 00000000 00000000 00000101
00000000 00000000 00000000 00000110
───────────────────────────
00000000 00000000 00000000 00000100
Vertical look, up and down are 1 The Get 1 , or else it is 0.
So $a & $b = 4.

~ $a not (bitwise non) sets the bit in the $a to 0 and vice versa.
~ $a:
00000000 00000000 00000000 00000101
───────────────────────────
11111111 11111111 11111111 11111010
If $a is a signed integer, the first bit is the sign bit, and 1 indicates a negative number, then ~ $a =-2147483642.
If $a is an unsigned integer, the first bit or the data bit, then ~ $a = 4294967290.

The following is not a detailed description:
$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.

Usually said a few of the system, is the system to deal with integers, the number of single-digit simultaneous calculation, the above example is based on 32-bit system. Because the bits that were moved over will be discarded, if it is 64-bit or 12-bit, the result of the displacement may be different, and the number of calculated time bits will be adjusted.

From:http://blog.sina.com.cn/s/blog_815611fb01017wtj.html

http://www.bkjia.com/PHPjc/735886.html www.bkjia.com true http://www.bkjia.com/PHPjc/735886.html techarticle this involves the bottom of the system, Web development is almost useless, know that the next thing is good. The underlying interpretation of things is always simple. Variables stored in memory are binary ...

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