This involves the bottom of the system, Web development is almost useless, know that the next thing is good. It's always easy to explain what's on the ground.
Variables are stored in memory as binary digits. The integer on the 32-bit system is occupied 4 bytes, a byte is 8 bits, that is 32 bits, bit operation is bit-level operation.

$a << $b shift left (move right) moves the bits in the $a to the left $b times (each time the move represents "Times 2").
Assume that the variable $a The value is 3, $b The value is 2.
So $a stored in memory in the format is:
00000000 00000000 00000000 00000011
Every time you move left, all the bits are moved to the left, the right is 0, and the left is over.
So $a Move left 2 Get is:
00000000 00000000 00000000 00011000
Is 12. Equivalent 3x2x2 = 12.

$a >> $b shift Right (move) moves the bits in the $a to the right $b times (each move is "divided by 2").
Assume that the variable $a The value is $b The value is 2.
So $a stored in memory in the format is:
00000000 00000000 00000000 00011000
Every time you move to the right, all the bits are moved, the right over is discarded, and the left space is 0.
So $a Move right 2 Get is:
00000000 00000000 00000000 00000110
Is 6. Equivalent 24÷2÷2 = 6.

The displacement operation is the system bottom operation, the computation speed is much faster than multiplication and division, the optimized code optimizes to the extreme time, may convert the number into two common multiple, then carries on the displacement computation. However, there is little need in web development, and more than the bottom of the system, the program will be less portable.

The other bitwise operators are also similar. Like what:
$a & $b and (bitwise) will set the $a and the bits of the $b 1 to 1.
Assume that the 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 To get 1 , otherwise it is 0.
So $a & $b = 4.

~ $a not (bitwise not) sets the bit of 0 in the $a to 1, and vice versa.
~ $a:
00000000 00000000 00000000 00000101
───────────────────────────
11111111 11111111 11111111 11111010
If $a is a signed integer, the first digit is the sign bit, and the 1 represents 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 covered in detail:
$a $b or (bitwise OR) will set the $a or a bit of 1 in the $b to 1.
$a ^ $b Xor (bitwise exclusive OR) sets the different bits in the $a and $b to 1.

Commonly said several systems, is the system processing integers, how many digits are computed at the same time, the above examples are based on 32-bit system. Because more than one bit will be discarded when moving, if it is 64-bit or 12-bit, the result of the displacement may be different, and the number of calculated times will be adjusted.

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.