位锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷指锟斤拷锟斤拷位锟斤拷锟斤拷锟斤拷值锟酵诧拷锟斤拷锟斤拷
锟斤拷锟斤拷 | 锟斤拷锟斤拷 | 锟斤拷锟� |
---|---|---|
$a & $b |
And锟斤拷锟斤拷位锟诫) | 锟斤拷锟斤拷 $a 锟斤拷 $b 锟叫讹拷为 1 锟斤拷位锟斤拷为 1锟斤拷 |
$a | $b |
Or锟斤拷锟斤拷位锟斤拷 | 锟斤拷锟斤拷 $a 锟斤拷 $b 锟斤拷锟轿猴拷一锟斤拷为 1 锟斤拷位锟斤拷为 1锟斤拷 |
$a ^ $b |
Xor锟斤拷锟斤拷位锟斤拷锟� | 锟斤拷锟斤拷 $a 锟斤拷 $b 锟斤拷一锟斤拷为 1 锟斤拷一锟斤拷为 0 锟斤拷位锟斤拷为 1锟斤拷 |
~ $a |
Not锟斤拷锟斤拷位取锟斤拷锟斤拷 | 锟斤拷 $a 锟斤拷为 0 锟斤拷位锟斤拷为 1锟斤拷锟斤拷之锟斤拷然锟斤拷 |
$a << $b |
Shift left锟斤拷锟斤拷锟狡o拷 | 锟斤拷 $a 锟叫碉拷位锟斤拷锟斤拷锟狡讹拷 $b 锟轿o拷每一锟斤拷锟狡讹拷锟斤拷锟斤拷示"锟斤拷锟斤拷 2"锟斤拷锟斤拷 |
$a >> $b |
Shift right锟斤拷锟斤拷锟狡o拷 | 锟斤拷 $a 锟叫碉拷位锟斤拷锟斤拷锟狡讹拷 $b 锟轿o拷每一锟斤拷锟狡讹拷锟斤拷锟斤拷示"锟斤拷锟斤拷 2"锟斤拷锟斤拷 |
位锟斤拷锟斤拷 PHP 锟斤拷锟斤拷锟斤拷学锟斤拷锟姐。锟斤拷锟轿何凤拷锟斤拷锟狡筹拷去锟斤拷位锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷时锟揭诧拷锟斤拷锟斤拷锟斤拷洌拷锟斤拷锟轿伙拷锟斤拷锟斤拷锟斤拷锟轿讹拷锟斤拷锟斤拷锟斤拷挪锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞憋拷锟斤拷锟皆凤拷锟斤拷位锟斤拷洌拷锟轿讹拷锟斤拷锟斤拷锟斤拷疟锟斤拷锟斤拷锟斤拷锟�
要锟斤拷锟斤拷锟斤拷确锟斤拷锟斤拷要锟斤拷锟斤拷锟饺硷拷锟斤拷锟斤拷锟斤拷 $a & $b == true 锟饺斤拷锟叫比斤拷锟劫斤拷锟叫帮拷位锟诫;锟斤拷 ($a & $b) == true 锟斤拷锟饺斤拷锟叫帮拷位锟斤拷锟劫斤拷锟叫比较★拷
If both operands for the &, | and ^ operators are strings, then the operation will be performed on the ASCII values of the characters that make up the strings and the result will be a string. In all other cases, both operands will be converted to integers and the result will be an integer.
If the operand for the ~ operator is a string, the operation will be performed on the ASCII values of the characters that make up the string and the result will be a string, otherwise the operand and the result will be treated as integers.
Both operands and the result for the << and >> operators are always treated as integers.
PHP 锟斤拷 ini 锟借定 error_reporting 使锟斤拷锟剿帮拷位锟斤拷值锟斤拷
锟结供锟剿关憋拷某锟斤拷位锟斤拷锟斤拷实锟斤拷锟接★拷要锟斤拷示锟斤拷锟斤拷锟斤拷示锟斤拷锟斤拷
之锟斤拷锟斤拷锟斤拷写锟斤拷锟絧hp.ini 锟斤拷锟斤拷锟斤拷锟斤拷锟矫的o拷
E_ALL & ~E_NOTICE
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷式锟斤拷锟斤拷取锟斤拷 E_ALL 锟斤拷值锟斤拷 00000000000000000111011111111111 锟斤拷取锟斤拷 E_NOTICE 锟斤拷值锟斤拷 00000000000000000000000000001000 然锟斤拷通锟斤拷 ~ 锟斤拷锟斤拷取锟斤拷锟斤拷 11111111111111111111111111110111 锟斤拷锟斤拷锟斤拷冒锟轿伙拷锟� AND锟斤拷&锟斤拷锟矫碉拷锟斤拷锟斤拷值锟叫讹拷锟借定锟剿o拷为 1锟斤拷锟斤拷位锟斤拷 00000000000000000111011111110111
锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟矫帮拷位锟斤拷锟� XOR锟斤拷^锟斤拷锟斤拷取锟斤拷只锟斤拷
锟斤拷锟斤拷一锟斤拷值锟斤拷锟借定锟剿碉拷位锟斤拷
E_ALL ^ E_NOTICE
error_reporting 也锟斤拷锟斤拷锟斤拷锟斤拷示锟斤拷锟斤拷锟斤拷位锟斤拷只锟斤拷示锟斤拷锟斤拷涂苫指锟�
锟斤拷锟斤拷姆锟斤拷锟斤拷牵锟�
E_ERROR | E_RECOVERABLE_ERROR
也锟斤拷锟角斤拷 E_ERROR 00000000000000000000000000000001 锟斤拷 E_RECOVERABLE_ERROR 00000000000000000001000000000000 锟矫帮拷位锟斤拷 OR锟斤拷|锟斤拷锟斤拷锟斤拷锟斤拷锟饺★拷锟斤拷锟斤拷魏锟揭伙拷锟街碉拷斜锟斤拷锟轿伙拷慕锟斤拷锟斤拷 00000000000000000001000000000001
Example #1 锟斤拷锟斤拷锟斤拷 AND锟斤拷OR 锟斤拷 XOR 位锟斤拷锟斤拷锟�
<?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 are the examples.
*/
$values = array(0, 1, 2, 4, 8);
$test = 1 + 4;
echo "\n Bitwise AND \n";
foreach ($values as $value) {
$result = $value & $test;
printf($format, $result, $value, '&', $test);
}
echo "\n Bitwise Inclusive OR \n";
foreach ($values as $value) {
$result = $value | $test;
printf($format, $result, $value, '|', $test);
}
echo "\n Bitwise Exclusive OR (XOR) \n";
foreach ($values as $value) {
$result = $value ^ $test;
printf($format, $result, $value, '^', $test);
}
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
--------- --------- -- --------- 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 = 1000) & ( 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 锟街凤拷锟斤拷锟斤拷 XOR 锟斤拷锟斤拷锟�
<?php
echo 12 ^ 9; // Outputs '5'
echo "12" ^ "9"; // Outputs the Backspace character (ascii 8)
// ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8
echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0
// 'a' ^ 'e' = #4
echo 2 ^ "3"; // Outputs 1
// 2 ^ ((int)"3") == 1
echo "2" ^ 3; // Outputs 1
// ((int)"2") ^ 3 == 1
?>
Example #3 锟斤拷锟斤拷锟斤拷位锟斤拷
<?php
/*
* Here are the examples.
*/
echo "\n--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---\n";
$val = 4;
$places = 1;
$res = $val >> $places;
p($res, $val, '>>', $places, 'copy of sign bit shifted into left side');
$val = 4;
$places = 2;
$res = $val >> $places;
p($res, $val, '>>', $places);
$val = 4;
$places = 3;
$res = $val >> $places;
p($res, $val, '>>', $places, 'bits shift out 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 into left side');
$val = -4;
$places = 2;
$res = $val >> $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, '<<', $places, '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, '<<', $places, 'bits shift out left side');
echo "\n--- BIT SHIFT LEFT ON NEGATIVE INTEGERS ---\n";
$val = -4;
$places = 1;
$res = $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 is just formatting to make output clearer.
*/
function p($res, $val, $op, $places, $note = '') {
$format = '%0' . (PHP_INT_SIZE * 8) . "b\n";
printf("Expression: %d = %d %s %d\n", $res, $val, $op, $places);
echo " Decimal:\n";
printf(" val=%d\n", $val);
printf(" res=%d\n", $res);
echo " Binary:\n";
printf(' val=' . $format, $val);
printf(' res=' . $format, $res);
if ($note) {
echo " NOTE: $note\n";
}
echo "\n";
}
?>
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 32 位锟斤拷锟斤拷锟较碉拷锟斤拷锟�:
--- BIT SHIFT RIGHT ON POSITIVE INTEGERS --- Expression: 2 = 4 >> 1 Decimal: val=4 res=2 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000010 NOTE: copy of sign bit shifted into left side Expression: 1 = 4 >> 2 Decimal: val=4 res=1 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000001 Expression: 0 = 4 >> 3 Decimal: val=4 res=0 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000000 NOTE: bits shift out right side Expression: 0 = 4 >> 4 Decimal: val=4 res=0 Binary: val=00000000000000000000000000000100 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 bit shifted into left side Expression: -1 = -4 >> 2 Decimal: val=-4 res=-1 Binary: val=11111111111111111111111111111100 res=11111111111111111111111111111111 NOTE: bits shift out right side Expression: -1 = -4 >> 3 Decimal: val=-4 res=-1 Binary: val=11111111111111111111111111111100 res=11111111111111111111111111111111 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 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000001000 NOTE: zeros fill in right side Expression: 1073741824 = 4 << 28 Decimal: val=4 res=1073741824 Binary: val=00000000000000000000000000000100 res=01000000000000000000000000000000 Expression: -2147483648 = 4 << 29 Decimal: val=4 res=-2147483648 Binary: val=00000000000000000000000000000100 res=10000000000000000000000000000000 NOTE: sign bits get shifted out Expression: 0 = 4 << 30 Decimal: 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 Binary: val=11111111111111111111111111111100 res=11111111111111111111111111111000 NOTE: zeros fill in right side Expression: -2147483648 = -4 << 29 Decimal: val=-4 res=-2147483648 Binary: val=11111111111111111111111111111100 res=10000000000000000000000000000000 Expression: 0 = -4 << 30 Decimal: val=-4 res=0 Binary: val=11111111111111111111111111111100 res=00000000000000000000000000000000 NOTE: bits shift out left side, including sign bit
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 64 位锟斤拷锟斤拷锟较碉拷锟斤拷锟�:
--- BIT SHIFT RIGHT ON POSITIVE INTEGERS --- Expression: 2 = 4 >> 1 Decimal: val=4 res=2 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000010 NOTE: copy of sign bit shifted into left side Expression: 1 = 4 >> 2 Decimal: val=4 res=1 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000001 Expression: 0 = 4 >> 3 Decimal: val=4 res=0 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000000 NOTE: bits shift out right side Expression: 0 = 4 >> 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=1111111111111111111111111111111111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111110 NOTE: copy of sign bit shifted into left side Expression: -1 = -4 >> 2 Decimal: val=-4 res=-1 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111111 NOTE: bits shift out right side Expression: -1 = -4 >> 3 Decimal: val=-4 res=-1 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 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 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000001000 NOTE: zeros fill in right side Expression: 4611686018427387904 = 4 << 60 Decimal: val=4 res=4611686018427387904 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0100000000000000000000000000000000000000000000000000000000000000 Expression: -9223372036854775808 = 4 << 61 Decimal: val=4 res=-9223372036854775808 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=1000000000000000000000000000000000000000000000000000000000000000 NOTE: sign bits get shifted out Expression: 0 = 4 << 62 Decimal: val=4 res=0 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000000 NOTE: bits shift out left side --- BIT SHIFT LEFT ON NEGATIVE INTEGERS --- Expression: -8 = -4 << 1 Decimal: val=-4 res=-8 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111000 NOTE: zeros fill in right side Expression: -9223372036854775808 = -4 << 61 Decimal: val=-4 res=-9223372036854775808 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1000000000000000000000000000000000000000000000000000000000000000 Expression: 0 = -4 << 62 Decimal: val=-4 res=0 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=0000000000000000000000000000000000000000000000000000000000000000 NOTE: bits shift out left side, including sign bit
Shifting integers by values greater than or equal to the system long integer width results in undefined behavior. In other words, don't shift more than 31 bits on a 32-bit system, and don't shift more than 63 bits on a 64-bit system.
使锟斤拷 gmp 锟斤拷展锟皆筹拷锟斤拷 PHP_INT_MAX 锟斤拷锟斤拷值锟斤拷锟斤拷锟斤拷位锟斤拷锟斤拷锟斤拷
锟轿硷拷 pack(), unpack(), gmp_and(), gmp_or(), gmp_xor(), gmp_testbit(), gmp_clrbit()