Bitwise operators allow you to evaluate and manipulate the bits specified in the integer number.
Bitwise operators
Example name result
$a & $b and (bitwise AND) will set the 1 bit in the $a and $b to 1.
$a | $b or (bitwise OR) sets any bit in the $a and the $b to 1 for 1.
$a ^ $b Xor (Bitwise XOR) sets the $a and $b one to 1 and the other 0 to 1.
~ $a not (bitwise negate) sets the bit in the $a to 0 and vice versa.
$a << $b Shift left moves the bits in the $a $b times (each move represents "multiplied by 2").
$a >> $b Shift right shifts the bits in the $a to the right $b times (each move represents "divided by 2").
Displacement is a mathematical operation in PHP. Bits that are moved in any direction are discarded. The right side of the left shift is filled with 0, and the sign bit is removed, which means the signs are not retained. When you move right, the left side is filled with a symbol bit, which means the sign is retained.
Use parentheses to ensure that you want the priority. For example $a & $b = = True to first compare and then bitwise AND ($a & $b) = = True to first bitwise and then compare.
Be aware of conversions of data types. If both the left and right arguments are strings, the bitwise operator operates on the ASCII value of the character.
PHP's INI setting error_reporting uses a bitwise value, providing a real example of closing a bit. To display all errors except the prompt level, this is used in php.ini:
E_all & ~e_notice
The way to do this is to get the value of E_all first: 00000000000000000111011111111111 and then get the value of E_notice: 00000000000000000000000000001000 and then through ~ To reverse: 11111111111111111111111111110111 and then use the bitwise AND and (&) to get two values set (1) bit: 00000000000000000111011111110111
Another method is to use bitwise XOR (^) to obtain only the
The bits set in one of the values:
E_all ^ E_notice
Error_reporting can also be used to demonstrate how to set the position. Show only errors and recoverable
The wrong method is:
E_error | E_recoverable_error
That is, e_error00000000000000000000000000000001 and e_recoverable_error00000000000000000001000000000000 with a bitwise OR OR (| ) operator to obtain the result of being set in any one of the values: 00000000000000000001000000000001
Example the And,or and XOR bitwise operators #1 integers
<?php/** Ignore The top section,* It is just formatting to make output clearer.*/$format = ' (%1$2d =%1$04b) = (%2$2d = %2$04B) '. '%3$s (%4$2d =%4$04b) '. "\ n"; Echo <<<eoh--------- --------- -----------result value op test--------- ------ --- -----------eoh;/** here is the examples.*/$values = Array (0, 1, 2, 4, 8); $test = 1 + 4;echo "\ n Bitwise and \ n"; foreach ($values as $value) {$result = $value & $test;p rintf ($format, $result, $value, ' & ', $test);} echo "\ n Bitwise Inclusive OR \ n"; foreach ($values as $value) {$result = $value | $test;p rintf ($format, $result, $value, ' | ', $test);} echo "\ n Bitwise Exclusive OR (XOR) \ n", foreach ($values as $value) {$result = $value ^ $test;p rintf ($format, $result, $va Lue, ' ^ ', $test);}? >
The above routines will output:
--------- --------- -----------result value op test--------- --------- ----------- Bitwise and (0 = 0000) = (0 = 0000) & (5 = 0101) (1 = 0001) = (1 = 0001) & (5 = 0101) (0 = 0000) = (2 = 0010) & (5 = 0101) (4 = 0100) = (4 = 0100) & (5 = 0101) (0 = 0000) = (8 = +) & (5 = 0101) Bitwise Inclusive OR (5 = 0101) = (0 = 0000) | (5 = 0101) (5 = 0101) = (1 = 0001) | (5 = 0101) (7 = 0111) = (2 = 0010) | (5 = 0101) (5 = 0101) = (4 = 0100) | (5 = 0101) (13 = 1101) = (8 = 1000) | (5 = 0101) Bitwise Exclusive OR (XOR) (5 = 0101) = (0 = 0000) ^ (5 = 0101) (4 = 0100) = (1 = 0001) ^ (5 = 0101) (7 = 0111) = (2 = 0010) ^ (5 = 0101) (1 = 0001) = (4 = 0100) ^ (5 = 0101) (13 = 1101) = (8 = 1000) ^ (5 = 0101)
Example #2 The XOR operator of a string
<?phpecho 12 ^ 9; Outputs ' 5 ' echo "12" ^ "9"; Outputs the Backspace character (ASCII 8)//(' 1 ' (ASCII)) ^ (' 9 ' (ASCII)) = #8echo "Hallo" ^ "Hello"; Outputs the ASCII values #0 #4 #0 #0 #0//' a ' ^ ' e ' = #4echo 2 ^ "3"; Outputs 1//2 ^ ((int) "3") = = 1echo "2" ^ 3; Outputs 1//((int) "2") ^ 3 = = 1?>
Displacement of Example #3 integers
<?php/** here is the Examples.*/echo "\ n---BIT SHIFT right on POSITIVE integers---\ n"; $val = 4; $places = 1; $res = $v Al >> $places;p($res, $val, ' >> ', $places, ' copy of sign bit shifted to left side '); $val = 4; $places = 2; $re s = $val >> $places;p($res, $val, ' >> ', $places); $val = 4; $places = 3; $res = $val >> $places;p(, $res L, ' >> ', $places, ' bits shift out of right side '); $val = 4; $places = 4; $res = $val >> $places;p($res, $val, ' > > ', $places, ' same result as above; Can not shift beyond 0 '), echo "\ n---BIT shift right on negative integers---\ n"; $val =-4; $places = 1; $res = $val >> ; $places;p($res, $val, ' >> ', $places, ' copy of sign bit shifted to left side '); $val =-4; $places = 2; $res = $val &G T;> $places;p($res, $val, ' >> ', $places, ' bits shift out right side '); $val =-4; $places = 3; $res = $val >> $places;p($res, $val, ' >> ', $places, ' same result as above; can not shift beyond-1 '); echo "\ n---BIT SHIFT left on POSITIVE integers---\ n "; $val = 4; $places = 1; $res = $val << $places;p($res, $val, ' << ', $pla CES, ' zeros fill in right side '); $val = 4; $places = (Php_int_size * 8)-4; $res = $val << $places;p($res, $val, ' < ;< ', $places); $val = 4; $places = (Php_int_size * 8)-3; $res = $val << $places;p($res, $val, ' << ', $places, ' Sign bits get shifted out '); $val = 4; $places = (Php_int_size * 8)-2; $res = $val << $places;p($res, $val, ' <&L t; ', $places, ' bits shift out of left side '), echo "\ n---BIT shift left on negative integers---\ n"; $val = 4; $places = 1; $re s = $val << $places;p($res, $val, ' << ', $places, ' zeros fill in right side '); $val =-4; $places = (php_int_size * 8)-3; $res = $val << $places;p($res, $val, ' << ', $places); $val =-4; $places = (Php_int_size * 8)-2; $res = $val << $places;p($res, $val, ' << ', $places, ' bits shift out left side, including sign bit ');/** Ignore This Bottom section,* It isJust formatting to make output clearer.*/function p ($res, $val, $op, $places, $note = ') {$format = '%0 '). (Php_int_size * 8). "B\n";p rintf ("Expression:%d =%d%s%d\n", $res, $val, $op, $places), echo "decimal:\n";p rintf ("val=%d\n", $val);p rintf ("res=%d\n", $res); echo "binary:\n";p rintf (' val= '. $format, $val);p rintf (' res= '. $format, $res); if ($note) {echo "Note: $note \ n";} echo "\ n";}? >
The output of the above routines on a 32-bit machine:
---BIT SHIFT right on POSITIVE integers---expression:2 = 4 >> 1 decimal:val=4 res=2 binary:val=0000000000000 0000000000000000100 res=00000000000000000000000000000010 note:copy of sign bit shifted to left Sideexpression:1 = 4 & Gt;> 2 decimal:val=4 Res=1 binary:val=00000000000000000000000000000100 res=00000000000000000000000000000001expre ssion:0 = 4 >> 3 decimal:val=4 res=0 binary:val=00000000000000000000000000000100 res=000000000000000000000000 00000000 Note:bits shift out Right sideexpression:0 = 4 >> 4 decimal:val=4 res=0 binary:val=00000000000000000 000000000000100 res=00000000000000000000000000000000 note:same result as above; Can not shift beyond 0---BIT shift right on negative integers---Expression:-2 =-4 >> 1 decimal:val=-4 res=-2 binary:val=11111111111111111111111111111100 res=11111111111111111111111111111110 note:copy of sign bits shifted into Le FT Sideexpression:-1 = 4 >> 2 decimal:val=-4 res=-1 Binary:val=11111111111111111111111111111100 res=11111111111111111111111111111111 note:bits shift out right Sideexpression:-1 = 4 >> 3 decimal:val=-4 res=-1 binary:val=11111111111111111111111111111100 res=11111111111 111111111111111111111 note:same result as above; Can not shift beyond-1---BIT shift left on POSITIVE integers---expression:8 = 4 << 1 decimal:val=4 res=8 Bina ry:val=00000000000000000000000000000100 res=00000000000000000000000000001000 Note:zeros fill in right sideExpression : 1073741824 = 4 << decimal:val=4 res=1073741824 binary:val=00000000000000000000000000000100 res=0100000000 0000000000000000000000Expression: -2147483648 = 4 << decimal:val=4 res=-2147483648 binary:val=00000000000000 000000000000000100 res=10000000000000000000000000000000 note:sign bits get shifted outexpression:0 = 4 << Deci mal:val=4 res=0 binary:val=00000000000000000000000000000100 res=00000000000000000000000000000000 Note:biTS shift out left side---BIT shift left on negative integers---Expression:-8 = 4 << 1 decimal:val=-4 res=-8 B inary:val=11111111111111111111111111111100 res=11111111111111111111111111111000 Note:zeros Fill in right Sideexpression: -2147483648 = 4 << decimal:val=-4 res=-2147483648 binary:val=111111111111111111111111111111 XX res=10000000000000000000000000000000expression:0 = 4 << decimal:val=-4 res=0 binary:val=11111111111111 111111111111111100 res=00000000000000000000000000000000 note:bits shift out left side, including sign bit
The output of the above routines on a 64-bit machine:
---BIT SHIFT right on POSITIVE integers---expression:2 = 4 >> 1 decimal:val=4 res=2 binary:val=0000000000000 000000000000000000000000000000000000000000000000100 res= 0000000000000000000000000000000000000000000000000000000000000010 note:copy of sign bit shifted to left Sideexpression:1 = 4 >> 2 decimal:val=4 Res=1 binary:val=00000000000000000000000000000000000000000000000000000 00000000100 res=0000000000000000000000000000000000000000000000000000000000000001expression:0 = 4 >> 3 Decimal: val=4 res=0 binary:val=0000000000000000000000000000000000000000000000000000000000000100 res= 0000000000000000000000000000000000000000000000000000000000000000 note:bits shift out Right sideexpression:0 = 4 >& Gt 4 decimal:val=4 res=0 binary:val=0000000000000000000000000000000000000000000000000000000000000100 res= 0000000000000000000000000000000000000000000000000000000000000000 note:same result as above; Can not shift beyond 0---BIT shift right on negative Integers---Expression:-2 = 4 >> 1 decimal:val=-4 res=-2 binary:val=1111111111111111111111111111111111111111 111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111110 note:copy of sign bit Shifted into left sideexpression:-1 = 4 >> 2 decimal:val=-4 res=-1 binary:val=1111111111111111111111111111111 111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111111 Note:bits Shift out right sideexpression:-1 = 4 >> 3 decimal:val=-4 res=-1 binary:val=111111111111111111111111111111111 1111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111111 Note:same result as above; Can not shift beyond-1---BIT shift left on POSITIVE integers---expression:8 = 4 << 1 decimal:val=4 res=8 Bina ry:val=0000000000000000000000000000000000000000000000000000000000000100 res= 0000000000000000000000000000000000000000000000000000000000001000 Note: Zeros fill in right sideexpression:4611686018427387904 = 4 << decimal:val=4 res=4611686018427387904 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res= 0100000000000000000000000000000000000000000000000000000000000000Expression: -9223372036854775808 = 4 << 61 Decimal:val=4 res=-9223372036854775808 binary:val= 0000000000000000000000000000000000000000000000000000000000000100 res= 1000000000000000000000000000000000000000000000000000000000000000 note:sign bits Get shifted outexpression:0 = 4 <& Lt decimal:val=4 res=0 binary:val=0000000000000000000000000000000000000000000000000000000000000100 res= 0000000000000000000000000000000000000000000000000000000000000000 note:bits shift out left side---BIT shift left on NEGA tive integers---Expression: 8 = 4 << 1 decimal:val=-4 res=-8 binary:val=111111111111111111111111111111111111 1111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111000 Note:zeros fill in right sideexpression: -9223372036854775808 = 4 << decimal:val=-4 res=-9223372036 854775808 binary:val=1111111111111111111111111111111111111111111111111111111111111100 res= 1000000000000000000000000000000000000000000000000000000000000000expression:0 =-4 << decimal:val=-4 res=0 Bi nary:val=1111111111111111111111111111111111111111111111111111111111111100 res= 0000000000000000000000000000000000000000000000000000000000000000 note:bits shift out left side, including sign bit
Warning
Do not move more than 32 bits to the right under the 32-bit system. Do not move left in cases where the result may exceed 32. Use the GMP extension to perform bit operations on values that exceed Php_int_max.