PHP shift operations, shift operation Learning notes

Source: Internet
Author: User
Tags bitwise bitwise operators mixed strlen

Application formula for bitwise operation

Clear 0 to use with, a location one available or

To reverse and exchange, gently loosen the different or

Shift Operations

Point 1 They are both binocular operators, and two of the operands are plastic, and the result is plastic.

2 "<<" left: Fill 0 on the right vacated position, the left bit will be squeezed from the head, the value of which is equal to 2.

3 ">>" Move right: the right side is squeezed out. For an empty space to be left out, if it is a positive number, the vacancy is 0, and a negative number may be 0 or 1, depending on the computer system used.

4 ">>>" operator, the right side is squeezed out, for the left to remove the empty space to fill up 0.

Application of bitwise operators (source operand s mask mask)

(1) Bitwise AND--&

1 Clear 0 Special location (mask 0, other bit 1,s=s&mask)

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

(2) Bitwise OR--|

It is often used to place the source operand in some position 1 and the other bits unchanged. (1 in mask, other bits 0 s=s|mask)

(3) A bit different or--^

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

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

Status after Target action operation

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: (x-y) ^ ((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) to determine whether an int variable A is odd or even

a&1 = 0 Even

a&1 = 1 Odd

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

(3) The K-position of the int type variable A is 0, namely a=a&~ (1<<K)

(4) A K position 1, or a=a|, of the INT type variable A (1<<k)

(5) INT-type variable cycle left K-time, that is a=a<<k|a>>16-k (set sizeof (int) =16)

(6) INT-type variable a cycle right shift k, that is a=a>>k|a<<16-k (set sizeof (int) =16)

(7) Average of integers

For two integer x,y, if you use (x+y)/2 to mean an overflow, because the x+y may be greater than Int_max, but we know that their average value is definitely not overflow, we use the following algorithm:

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

{

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

}

(8) to 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) Do not exchange two integers without temp

void swap (int x, int y)

{

x ^= y;

Y ^= x;

x ^= y;

}

(10) Calculating absolute value

int abs (int x)

{

int y;

y = x >> 31;

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

}

(11) Conversion of modulo operation into bit operation (without overflow)

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

(12) The multiplication operation transforms into the bit operation (in the case that does not produce overflow)

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

(13) Conversion of division operations into bit operations (without overflow)

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

Example: 12/8 = = 12>>3

(a)% 2 is equivalent to A & 1

(a) 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 information about the bits shift operation


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

Because PHP has only 32-bit signed integers, no 64-bit long integer, and no unsigned integers. Its integer range is -231-1~231, which is beyond this range and will be interpreted as a floating-point number. So 0xFFFFFFFF, print directly, showing 4294967295, and 232:


>> 0xFFFFFFFFF
4294967295
>> GetType (0xFFFFFFFF)
' Double '


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


>> (int) 0xFFFFFFFFF
-1


PHP does not support the bits shift operation of floating-point numbers, and if it is to be done, it is first converted to an integral type, and the final result is returned according to the integral type:


>> 1 << 31
-2147483648
>> 1 << 30
1073741824
>> 1 << 32
1
>> 0xFFFFFFFF >> 1
-1


At the same time PHP's right shift operation, the high position will fill the symbol bit, and PHP does not provide Java-like >>> to force population 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 math function library, directly handle the string representation of integers, or gmp/bigint extension, and so on. But I think since using strings, then I can be a complete string, convert the number into 32 binary strings, 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, I did not do here)

Php

The code is as follows Copy Code
<?php
/**
* 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 number is returned if the integer range is exceeded
*/
function Shr32 ($x, $bits) {
Two cases of displacement exceeding range
if ($bits <= 0) {
return $x;
}
if ($bits >= 32) {
return 0;
}
Convert to a string that represents a binary number
$bin = Decbin ($x);
$l = strlen ($bin);
The string length exceeds 32 bits at the bottom of the intercept, the length is not enough, then the padding 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 with 0
Return Bindec (substr ($bin, 0, 32-$bits), Str_pad, ' 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 number is returned if the integer range is exceeded
*/
function Shl32 ($x, $bits) {
Two cases of displacement exceeding range
if ($bits <= 0) {
return $x;
}
if ($bits >= 32) {
return 0;
}
Convert to a string that represents a binary number
$bin = Decbin ($x);
$l = strlen ($bin);
The string length exceeds 32 bits at the bottom of the intercept, the length is not enough, then the padding 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 0 on the right
Return Bindec (substr ($bin, $bits), Str_pad, ' 0 ', str_pad_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.