Through an example description:
<?PHP//1. The value on either side of the bitwise operator must be both shaped and floating, and when it is a value of another type, it is converted to shaping and floating point to participate in bitwise operations;//The value of the operation on either side of the logical operator must be boolean; Var_dump (12&13);//Output int 12 12 the binary number converted to 32 bits is: 00000000 00000000 00000000 00001100//13 the binary number converted to 32 bits is: 00000000 00000000 00000000 00001101//bitwise operation The value obtained is 00000000 00000000 00000000 00001100, equal to var_dump (' A ' & ' a ');//output string ' a '; because a=65,a=97 var_dump (' A ' &97);//output int 0, because string A is converted to integer 0 and then participates in bit Operation //2. There is no short-circuit characteristic between bit operation and logic operation $a =3; $b =10; if ($a >5&& $b ++<100) { echo "1111111111";} echo $b. " <br> ";//output $b=10, logic operation Short Circuit, $b not self-added, if ($a >5& $b ++<100) { echo" 1111111 ";} echo $b." <br> ";//output $b=11, bit operation is not short-circuit, $b self-added; //3. Bitwise NON, bitwise XOR, etc. is relatively simple, here do not do too much Apple told; now talk about bitwise left SHIFT and bitwise Right Move var_dump (12>>2);//Output int 3 Var_dump (12<<2);//Output int 48//Can be found, the number of left to be multiplied by 2 of how many times, the bitwise right shift is equivalent to dividing by 2 of the number of sides;?>
(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<
<>
(4) Place the k position of the int variable a 1, i.e. a=a| (1<
<>
(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 >>; 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)
Do not move more than 32 bits on a 32-bit system, and do not move left if the result is likely to exceed 32 bits