PHP shift operator (<< shift left and >> right)

Source: Internet
Author: User
Tags bitwise

Displacement operator

<< left-shift

The essence of the left shift operation is to shift the binary value of the corresponding data to the left bit by bit, and fill 0 in the vacated position, the highest bit overflow and discard. For example
$a = 10;
$b = $a <<2;
Then $b=40, according to the manual description can see the bitwise operation can be seen to move one bit to the left, is to implement multiply 2 operation. The operation speed of the displacement operations is much higher than that of multiplication. Therefore, when the multiplication operation of the data is processed, the displacement operation can be used to obtain a faster speed.
Tip The conversion of all 2 multiplication operations to the displacement operation can improve the efficiency of the program operation.

Example:
The following three ways of expression are one meaning.
$a = 1024;
for ($i =1; $i < $a; $i = $i + $i) {
echo $i. " \ n ";
}

$a = 1024;
for ($i =1; $i < $a; $i = $i) {
echo $i. " \ n ";
}

$a = 1024;
for ($i =1; $i < $a; $i = $i <<1) {
echo $i. " \ n ";
}

>> bit Right Shift
The essence of the right shift operation is to shift the binary values of the corresponding data to the right bit by bit, and discard the numbers that are out of bounds. If the current number is an unsigned number, the high is 0. For example:
$a = 25;//11001
b=a>>2;//equivalent to: 11001 >> 01100, 01100 >> 00110. Then 110 = "6, i.e. 25/4 = 6
b= (0000 0000 0000 0110) =6
If the current data is a signed number, when the right shift is made, the left 0 or 1 is determined by the sign bit.
If the sign bit is 0, the left side is 0, but if the sign bit is 1, depending on the computer system, there may be different processing methods.
You can see that the bitwise right SHIFT operation can be achieved by dividing the divisor to 2.

Prompt to convert all of the 2 Division operations into displacement operations, which can improve the efficiency of the program operation

Example: Enter an integer to determine how many bits 1 are in this number? For example, enter 67 and the output should be 3.
Since the corresponding binary number of 67 is 00000000 01000011 (0043H), there are 3 1 occurrences.
Analysis: To judge is not 1, only need to judge the bit with 1 and later is not 1 can be known. An integer that can be judged 16 times.
Main () {
int num,k;
int count=0;/* number of records 1 */
scanf (%d,&num);
for (k=0;k<16;k++) {
if (num&1==1) count++; /* Determine if the lowest bit is 1 */
num>>=1;/* num Right Shift 1 bit */
}
printf (%d\n,count);
}

So every time you judge the lowest bit is not 1, after judging, let the front of the right to move one can.

PHP shift operator (<< shift left and >> right)

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.