Here are some common about PHP shift operations, shift operation Learning notes, I hope the article to the students bring value.

**The application of bitwise arithmetic to Formulas**

Clear 0 take a bit to use with, a position one is available or

To reverse and swap, easily use XOR or

**Shift Operations**

Point 1 They are both binocular operators, two operation components are shaped, and the result is shaping.

2 "<<" Shift left: fill 0 on the right-hand side, and the left bit will be squeezed out of the head, and its value is equal to 2.

3 ">>" right Shift: the right bit is squeezed out. For the left-hand exit, if it is a positive number, the vacancy is 0, if it is negative, it may be 0 or 1, depending on the computer system used.

4 ">>>" operator, the right bit is squeezed out, for the left side to move out of the vacancy is 0.

**The application of the bitwise operator (source operand s mask mask)**

(1) Bitwise AND--&

1 Clear 0 Special positioning (mask in the specific position 0, the other bit is 1,s=s&mask)

2 Take a number of the middle finger positioning (mask in a particular position 1, the other bit is 0,s=s&mask)

(2) Bitwise OR--|

It is commonly used to set the source operand to some location 1, other bits unchanged. (Mask in a specific position 1, the other bit is 0 s=s|mask)

(3) Bit XOR--^

1 reverse the value of a particular bit (at a specific position in mask 1, the other bit is 0 s=s^mask)

2 without introducing a third variable, swap the value of two variables (set A=A1,B=B1)

Target Operation Post-operation status

A=A1^B1 a=a^b A=A1^B1,B=B1

B=A1^B1^B1 b=a^b A=A1^B1,B=A1

A=B1^A1^A1 a=a^b A=B1,B=A1

**Binary complement Operation formula:**

-X = ~x + 1 = ~ (x-1)

~x =-x-1

-(~x) = X+1

~ (-X) = X-1

X+y = x-~y-1 = (x|y) + (x&y)

XY = x + ~y + 1 = (x|~y)-(~x&y)

X^y = (x|y)-(X&y)

X|y = (x&~y) +y

X&y = (~x|y)-~x

X==y: ~ (x-y|y-x)

X!=y:x-y|y-x

x< y: (XY) ^ ((x^y) & ((x-y) ^x))

X<=y: (X|~y) & ((x^y) |~ (y-x))

x< y: (~x&y) | ((~x|y) & (x-y))//unsigned x, y comparison

X<=y: (~x|y) & ((x^y) |~ (y-x))//unsigned x, y comparison

**Application examples**

(1) determine whether the int variable A is an odd or an even number

a&1 = 0 Even

a&1 = 1 Odd

(2) Take the K-bit (k=0,1,2......sizeof (int)) of the type int variable A, i.e. a>>k&1

(3) The K-position of the int variable A is cleared 0, i.e. a=a&~ (1<<>< p=""><>

(4) Place the k position of the int variable a 1, i.e. a=a| (1<<>< p=""><>

(5) int type variable loop left shift k times, namely a=a< >16-k (set sizeof (int) =16)

(6) int type variable a loop right shift k times, namely A=a>>k|a<<16-k (set sizeof (int) =16)

(7) Average of integers

For two integers x, y, if averaging is used (x+y)/2, an overflow is generated because the x+y may be greater than Int_max, but we know that their average is definitely not overflow, we use the following algorithm:

int average (int x, int y)//Returns the mean of X, y

{

Return (X&y) + ((x^y) >>1);

}

(8) Determine whether an integer is a power of 2, for a number x >= 0, to determine whether he is a power of 2

Boolean power2 (int x)

{

Return ((x& (x-1)) ==0) && (x!=0);

}

(9) Swap two integers without temp

void swap (int x, int y)

{

x ^= y;

Y ^= x;

x ^= y;

}

(10) Calculate absolute value

int abs (int x)

{

int y;

y = x >> 31;

Return (x^y)-y; Or: (x+y) ^y

}

(11) The modulo operation is converted into a bitwise operation (in the case of no overflow)

A% (2^n) equivalent to A & (2^n-1)

(12) The multiplication operation is converted into a bitwise operation (in the case of no overflow)

A * (2^n) equivalent to a<< n

(13) The division operation translates into a bitwise operation (in the case of no overflow)

A/(2^n) equivalent to a>> n

Example: 12/8 = = 12>>3

(+) A% 2 equivalent to A & 1

if (x = = a) x= b;

else x= A;

Equivalent to x= a ^ b ^ x;

(x) The opposite number is expressed as (~x+1)

**Finally add some about bits move operation**

PHP is mainly designed for text operations, in fact, PHP is not suitable for mathematical operations, efficiency is not high, but because this project has a thing must use the binary displacement operation, in PHP encountered some trouble.

Because PHP has only 32-bit signed integers, there are no 64-bit long integers, and there is no unsigned integer. The range of its integral type is -231-1~231, beyond which it will be interpreted as a floating-point number. Therefore, 0xFFFFFFFF, direct printing, shows the 4294967295, and 232:

>> 0xFFFFFFFFF

4294967295

>> GetType (0xFFFFFFFF)

' Double '

In a 32-bit signed integer, 0xFFFFFFFF should represent-1:

>> (int) 0xFFFFFFFFF

-1

While PHP does not support floating-point bits, if it is to be done, it will first be converted to an integer, and the final result will be returned as an integer type:

>> 1 << 31

-2147483648

>> 1 << 30

1073741824

>> 1 << 32

1

>> 0xFFFFFFFF >> 1

-1

At the same time, the right shift operation of PHP, the high will fill the sign bit, and PHP does not provide Java-like >>> to force the fill 0:

>> 1 << 32

1

>> 0xFFFFFFFF >> 1

-1

>> 0xFFFFFFFF >> 2

-1

>> 0xFFFFFFFF >> 3

-1

>> 0xFFFFFFFF >> 31

-1

How to solve this problem, I have considered using the Bcmath mathematical function library, directly processing the string representation of the integer, or the gmp/bigint extension and so on. But I think since the use of strings, then I can complete the string, convert the number to 32 binary string, and then manually fill 0, and finally converted back.

Don't know who has a better way, please tell me.

The code is as follows:

Direct download code: shift.php

(in addition, the code can be extended to any bit 2 of the displacement operation, here I did not do)

Php

The code is as follows |
Copy Code |

/** * Unsigned 32-bit right shift * @param mixed $x The number to be manipulated, if it is a string, it must be in decimal form * @param string $bits right Shift number * @return Mixed result, floating-point numbers will be returned if the integer range is exceeded */ function Shr32 ($x, $bits) { Two cases where displacement is out of range if ($bits <= 0) { return $x; } if ($bits >= 32) { return 0; } Convert to a string representing a binary number $bin = Decbin ($x); $l = strlen ($bin); The length of the string exceeds the bottom 32 bits, the length is not enough, then the fill high is 0 to 32 bits if ($l > 32) { $bin = substr ($bin, $l-32, 32); }elseif ($l < 32) { $bin = Str_pad ($bin, +, ' 0 ', str_pad_left); } Remove the number of digits to move and fill the left 0 Return Bindec (Str_pad (substr ($bin, 0, 32-$bits), +, ' 0 ', str_pad_left)); } /** * Unsigned 32-bit left shift * @param mixed $x The number to be manipulated, if it is a string, it must be in decimal form * @param string $bits left shift number * @return Mixed result, floating-point numbers will be returned if the integer range is exceeded */ function Shl32 ($x, $bits) { Two cases where displacement is out of range if ($bits <= 0) { return $x; } if ($bits >= 32) { return 0; } Convert to a string representing a binary number $bin = Decbin ($x); $l = strlen ($bin); The length of the string exceeds the bottom 32 bits, the length is not enough, then the fill high is 0 to 32 bits if ($l > 32) { $bin = substr ($bin, $l-32, 32); }elseif ($l < 32) { $bin = Str_pad ($bin, +, ' 0 ', str_pad_left); } Remove the number of digits you want to move and fill the right 0 Return Bindec (Str_pad (substr ($bin, $bits), +, ' 0 ', str_pad_right)); } |

http://www.bkjia.com/PHPjc/632618.html www.bkjia.com true http://www.bkjia.com/PHPjc/632618.html techarticle here are some common about PHP shift operations, shift operation Learning notes, I hope the article to the students bring value. Bit operation application 0 take bit to use with, a position one available or ...

**