PHP shift operator ____php

Source: Internet
Author: User
Tags bitwise bitwise operators

shift operator
The shift operator is the translation of numbers on the basis of binary. According to the direction of the translation and the rules of padding numbers are divided into three kinds of:<< (left), >> (with symbol right move) and >>> (unsigned Right move).
In shift operations, the results of the byte, short, and char types shift to the int type, and when the byte, short, char, and int are shifted, the actual number of moves is the amount of movement and the remainder of the 32, which is the same result as the shift 33 times and the displacement 1 times. When you move a long value, you specify that the number of moves is the number of moves and the remainder of the 64, which is the same result of moving 66 times and moving 2 times.
The move rules for the three shift operators and their use are shown below:
<< operation rules: In binary form, all the digits to the left to move the corresponding number of digits, high move out (discard), low vacancy of 0.
syntax Format:
Number of shifts << shifts required
For example: 3 << 2, then move the number 3 to the left 2 digits
calculation process:
3 << 2
First, convert 3 to binary number 0000 0000 0000 0000 0000 0000 0000 0011, and then move two 0 of the high (left), and the other digits to the left to translate 2, and then two for the low (right) position. The resulting end result is 0000 0000 0000 0000 0000 0000 0000 1100, then the conversion to decimal is 12. Mathematical Significance:
In the absence of overflow, for both positive and negative numbers, the left one is equal to the 1 times 2, and the left-shifted n-bit is the equivalent of multiplying by 2 N-Th.
>> operation rules: According to the binary form of all the numbers to the right to move the corresponding towering digits, low move out (discard), high vacancy complement symbol bit, that is, positive 0, negative 1.
syntax Format:
Number of shifts >> shifts required
For example one >> 2, move the number 11 to the right 2 bit
The calculation process: 11 of the binary form is: 0000 0000 0000 0000 0000 0000 0000 1011, and then move the last two digits out of the low, because the number is positive, so in the high position to fill 0. The resulting end result is 0000 0000 0000 0000 0000 0000 0000 0010. Convert to Decimal is 3. Mathematical significance: The right one is the equivalent of 2, the right to move N is the equivalent of dividing by 2 of the n-th side.
>>> operation rules: According to the binary form of all the numbers to the right to move the corresponding towering digits, low move out (discard), high vacancy of 0. For positive numbers, it is the same as with the signed right, which is different for negative numbers.
Other structures and >> are similar.
Summary
binary operators, including bitwise operators and shift operators, enable programmers to manipulate numbers on a binary basis, to perform operations more efficiently, and to store and transform data in binary form, which is the basis of algorithms for network protocol parsing and encryption.

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)

Java instance operations: public class urshift {         public  Static void main (String[] args)  {        int  i = -1;         i >>>= 10;         //system.out.println (i);         mtest ();     }          public static void  Mtest () {        //left          int i = 12; //binary is: 0000000000000000000000000001100          i <<= 2; //i left 2 digits, leaving the high two digits (left), the low vacancy filling 0, The binary code is 0000000000000000000000000110000         system.out.println (i);  //binary 110000 value of    &nbsP;    system.out.println ("<br>");                  //Right Shift          i >>=2; //i to the right 2 to, the low two digits (the right to start) discarded, high integer complement 0, negative 1, The binary code is 0000000000000000000000000001100         system.out.println (i);  //binary code is 1100 value of         system.out.println ("<br>");                  //Right Shift example         int j = 11;// Binary code for 00000000000000000000000000001011         j >>= 2;  //two-bit, discard the last two digits, integer complement 0, binary code: 00000000000000000000000000000010          system.out.println (j);  //binary code is 10 value of 2          System.out.println ("&Lt;br> ");                  byte k  = -2; //to int, binary code: 0000000000000000000000000000010          k >>= 2; //2-bit right, discard the last 2 digits, negative 1, binary?: 11000000000000000000000000000          system.out.println (k);  //binary is 11 value 2     }      }

The

shift operator is oriented to an Operation object that is also a binary "bit". You can use them individually to handle integer types (one of the main types). The left shift operator (<<) is able to move the operand on the left side of the operator to the right of the specified number of digits on the right-hand side of the operator (0 in low). The signed right shift operator (>>) assigns the Operation object to the left of the operator to the right of the specified number of digits to the right-hand operator. The "signed" right shift operator uses the symbol extension: If the value is positive, insert 0 at the high level, or 1 in the high position if the value is negative. Java also adds a "unsigned" right shift operator (>>>), which uses "0 extensions": Inserts 0 at high levels, either positive or negative. This operator is not in C or C + +.  
    If char,byte or short are shifted, they are automatically converted to an int before the shift occurs. Only 5 lows to the right are used. This prevents us from moving an unrealistic number of digits in an int. If a long value is processed, the resulting result is long. Only the 6 lows on the right are used at this point to prevent moving more than the number of bits in the Long value. However, you may also encounter a problem when you make an unsigned right shift. The right shift operation for byte or short values may not be the correct result (Java   1.0 and Java   1.1 are particularly prominent). They are automatically converted to type int and are shifted to the right. But "0 extensions" don't happen, so in those cases you get the result of 1.

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.