Php shift operation and shift operation learning Notes

Source: Internet
Author: User
Bitwise operations are used and when bit resetting is used, when a location can be used or if reverse and exchange are to be obtained, it is easy to use an exclusive or shift operation. Point 1: they are binary operators, and the two computation components are integers.

Bit operation application tips

The bitwise is to be used with or at a specified position.

To reverse and exchange, you can easily use an exception or

Shift operation

Key points

1. they are all binary operators. Both calculation components are integer and the result is also an integer.

2 "<" left shift: fill 0 on the right blank bit. the left bit will be squeezed out from the header, and its value is equal to 2.

3 ">" right shift: the right bit is squeezed out. For the vacant positions removed from the left, if it is a positive number, the vacant space is supplemented with 0. if it is a negative number, it may be supplemented with 0 or 1, depending on the computer system used.

4 ">>>" operator, the right bit is squeezed out, and 0 is added for the left Removed space.

Application of bitwise operators (source operand s mask)

(1) bitwise and --&

1. reset the special location (0 for a specific position in the mask, 1 for other bits, s = s & mask)

2. locate a specified position in a specified number (1 in mask, 0 for other bits, s = s & mask)

(2) bitwise OR -- |

It is often used to set the source operand to some positions of 1, and the other bits remain unchanged. (In mask, the specific position is 1, and the other bit is 0 s = s | mask)

(3) bitwise OR -- ^

1. reverse the value of the specific location (1 in a specific position in mask and 0 s = s ^ mask in other bits)

2. if the third variable is not introduced, the values of the two variables are exchanged (set a = a1, B = b1)

Status after 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 calculation formula:

-X = ~ X + 1 = ~ X-1)

~ X =-X-1

-(~ X) = x + 1

~ (-X) = x-1

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

X-y = 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) // comparison of unsigned x and y

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

Application example

(1) judge whether int variable a is an odd or even number.

A & 1 = 0 even

A & 1 = 1 Odd

(2) take the k-bit (k =, 2…) Of int-type variable ...... Sizeof (int), that is, a> k & 1

(3) clear the k bit of int variable a by 0, that is, a = &~ (1 <

(4) place the k position of int type variable a 1, that is, a = a | (1 <

(5) int type variable loops are shifted k times left, that is, a = a < > 16-k (set sizeof (int) = 16)

(6) int type variable a shifts k times to the right, that is, a = a> k | a <16-k (set sizeof (int) = 16)

(7) average integer

If two integers x and y are used to calculate the average value (x + y)/2, the overflow will occur, because x + y may be greater than INT_MAX, however, we know that their average values will certainly not overflow. We use the following algorithm:

  1. Int average (int x, int y) // returns the average value of X and Y.
  2. {
  3. Return (x & y) + (x ^ y)> 1 );
  4. }

(8) judge whether an integer is the power of 2. for a number x> = 0, determine whether it is the power of 2.

  1. Boolean power2 (int x)
  2. {
  3. Return (x & (x-1) = 0) & (x! = 0 );
  4. }

(9) two integers are not exchanged using temp.

  1. Void swap (int x, int y)
  2. {
  3. X ^ = y;
  4. Y ^ = x;
  5. X ^ = y;
  6. }

(10) calculate the absolute value

  1. Int abs (int x)
  2. {
  3. Int y;
  4. Y = x> 31;
  5. Return (x ^ y)-y; // or: (x + y) ^ y
  6. }

(11) modulo operation into bitwise operation (without overflow)

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

(12) multiplication is converted into bitwise operations (without overflow)

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

(13) division operations are converted into bitwise operations (without overflow)

A/(2 ^ n) is equivalent to a> n examples: 12/8 = 12> 3

(14) a % 2 is equivalent to a & 1

(15) if (x = a) x = B;

Else x = a; equivalent to x = a ^ B ^ x;

(16) the opposite number of x is expressed (~ X + 1)

Finally, add some binary displacement operations.

PHP is mainly designed for text operations. In fact, PHP is not suitable for mathematical operations and is inefficient. However, this project has something that must be used for binary displacement operations, I encountered some trouble in PHP.

Because PHP only has 32-bit signed integers, neither 64-bit long integers nor unsigned integers. The integer range is-231-1 ~ 231, beyond this range, will be interpreted as floating point. Therefore, 0 xFFFFFFFF is printed directly, and the display is 4294967295, and 232:

> 0 xFFFFFFFFF

4294967295

> Gettype (0 xFFFFFFFF)

'Double'

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

> (Int) 0 xFFFFFFFFF

-1

PHP does not support binary displacement of floating point numbers. if you want to perform this operation, it will first convert it to an integer type. The final result will also be returned according to the integer type:

  1. > 1 <31
  2. -2147483648
  3. > 1 <30
  4. 1073741824
  5. > 1 <32
  6. 1
  7. > 0 xFFFFFFFF> 1
  8. -1

At the same time, PHP's right displacement operation will fill the sign bit in the high position, and PHP does not provide a Java-like >>> to force fill 0:

  1. > 1 <32
  2. 1
  3. > 0 xFFFFFFFF> 1
  4. -1
  5. > 0 xFFFFFFFF> 2
  6. -1
  7. > 0 xFFFFFFFF> 3
  8. -1
  9. > 0 xFFFFFFFF> 31
  10. -1

How can I solve this problem? I have considered using the BCMath function library to directly process integers expressed by strings, or GMP/BigInt extensions. However, since I want to use strings, I can thoroughly convert the numbers into 32 binary strings, then manually fill them with 0, and finally convert them back.

I don't know which one has a better method. please let me know. In addition, the code can be extended to any bitwise binary displacement operation. I didn't do it here. the PHP code is as follows:

  1. /**
  2. * Unsigned 32-bit right shift
  3. * @ Param mixed $ x indicates the number to be operated. if it is a string, it must be in decimal format.
  4. * @ Param string $ bits right-shifted digits
  5. * @ Return mixed result. if the integer range is exceeded, the floating point number is returned.
  6. */
  7. Function shr32 ($ x, $ bits ){
  8. // Two situations in which the displacement exceeds the range
  9. If ($ bits <= 0 ){
  10. Return $ x;
  11. }
  12. If ($ bits> = 32 ){
  13. Return 0;
  14. }
  15. // Convert to a string that represents a binary number
  16. $ Bin = decbin ($ x );
  17. $ L = strlen ($ bin );
  18. // If the length of the string exceeds the limit, the minimum 32 bits are intercepted. if the length is not enough, the padding height is 0 to 32 bits.
  19. If ($ l> 32 ){
  20. $ Bin = substr ($ bin, $ l-32, 32 );
  21. } Elseif ($ l <32 ){
  22. $ Bin = str_pad ($ bin, 32, '0', STR_PAD_LEFT );
  23. }
  24. // Retrieve the number of digits to be moved and fill it with 0 on the left
  25. Return bindec (str_pad (substr ($ bin, 0, 32-$ bits), 32, '0', STR_PAD_LEFT ));
  26. }
  27. /**
  28. * Unsigned 32-bit left shift
  29. * @ Param mixed $ x indicates the number to be operated. if it is a string, it must be in decimal format.
  30. * @ Param string $ bits left-shifted digits
  31. * @ Return mixed result. if the integer range is exceeded, the floating point number is returned.
  32. */
  33. Function shl32 ($ x, $ bits ){
  34. // Two situations in which the displacement exceeds the range
  35. If ($ bits <= 0 ){
  36. Return $ x;
  37. }
  38. If ($ bits> = 32 ){
  39. Return 0;
  40. }
  41. // Convert to a string that represents a binary number
  42. $ Bin = decbin ($ x );
  43. $ L = strlen ($ bin );
  44. // If the length of the string exceeds the limit, the minimum 32 bits are intercepted. if the length is not enough, the padding height is 0 to 32 bits.
  45. If ($ l> 32 ){
  46. $ Bin = substr ($ bin, $ l-32, 32 );
  47. } Elseif ($ l <32 ){
  48. $ Bin = str_pad ($ bin, 32, '0', STR_PAD_LEFT );
  49. }
  50. // Retrieve the number of digits to be moved and fill 0 in the right
  51. Return bindec (str_pad (substr ($ bin, $ bits), 32, '0', STR_PAD_RIGHT ));
  52. }
  53. ?>

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.