PHP位运算符

WBOY
Lepaskan: 2016-07-30 13:30:44
asal
1268 orang telah melayarinya

位运算符允许对整型数中指定的位进行求值和操作。

位运算符
例子 名称 结果
$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 Shift left(左移) 将 $a 中的位向左移动 $b 次(每一次移动都表示“乘以 2”)。
$a >> $b Shift right(右移) 将 $a 中的位向右移动 $b 次(每一次移动都表示“除以 2”)。

位移在 PHP 中是数学运算。向任何方向移出去的位都被丢弃。左移时右侧以零填充,符号位被移走意味着正负号不被保留。右移时左侧以符号位填充,意味着正负号被保留。

要用括号确保想要的优先级。例如 $a & $b == true 先进行比较再进行按位与;而 ($a & $b) == true 则先进行按位与再进行比较。

要注意数据类型的转换。如果左右参数都是字符串,则位运算符将对字符的 ASCII 值进行操作。

PHP 的 ini 设定 error_reporting 使用了按位的值,
提供了关闭某个位的真实例子。要显示除了提示级别
之外的所有错误,php.ini 中是这样用的:
<span><codefira mono code pro word-wrap:break-word>E_ALL & ~E_NOTICE</codefira></span>
Salin selepas log masuk
具体运作方式是先取得 E_ALL 的值:
<span>00000000000000000111011111111111</span>
再取得 E_NOTICE 的值:
<span>00000000000000000000000000001000</span>
然后通过 <span>~</span> 将其取反:
<span>11111111111111111111111111110111</span>
最后再用按位与 AND(&)得到两个值中都设定了(为 1)的位:
<span>00000000000000000111011111110111</span>
Salin selepas log masuk
另外一个方法是用按位异或 XOR(<span>^</span>)来取得只在
其中一个值中设定了的位:
<span><codefira mono code pro word-wrap:break-word>E_ALL ^ E_NOTICE</codefira></span>
Salin selepas log masuk

error_reporting 也可用来演示怎样置位。只显示错误和可恢复
错误的方法是:
<span><codefira mono code pro word-wrap:break-word>E_ERROR | E_RECOVERABLE_ERROR</codefira></span>
Salin selepas log masuk
也就是将 E_ERROR
<span>00000000000000000000000000000001</span>
和 E_RECOVERABLE_ERROR
<span>00000000000000000001000000000000</span>
用按位或 OR(<span>|</span>)运算符来取得在任何一个值中被置位的结果:
<span>00000000000000000001000000000001</span>
Salin selepas log masuk

Example #1 整数的 AND,OR 和 XOR 位运算符

/*
 * 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 
 ---------     ---------  -- ---------
 result        value      op test
 ---------     ---------  -- ---------
EOH;
/*
 * Here are the examples.
 */
$values = array(01248);
$test 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)
Salin selepas log masuk

Example #2 字符串的 XOR 运算符

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 "3"// Outputs 1
              // 2 ^ ((int)"3") == 1
echo "2" 3// Outputs 1
              // ((int)"2") ^ 3 == 1
?>

Example #3 整数的位移

/*
 * 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 <p></p><p>以上例程在 64 位机器上的输出:</p><p></p><p></p><pre class="brush:php;toolbar:false">--- 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 <p><span>Warning</span></p><p>不要在 32 位系统下向右移超过 32 位。不要在结果可能超过 32 的情况下左移。使用 gmp 扩展对超出 PHP_INT_MAX 的数值来进行位操作。</p><p>参见 <span>pack()</span>,<span>unpack()</span>,<span>gmp_and()</span>,<span>gmp_or()</span>,<span>gmp_xor()</span>,<span>gmp_testbit()</span> 和 <span>gmp_clrbit()</span>。</p><p><span><img src="http://image.codes51.com/Article/image/20150824/20150824204951_7209.png" alt="add a note"    style="max-width:90%"  style="max-width:90%"> <small>add
 a note</small></span></p><p>User Contributed Notes <span>43 notes</span></p><p></p><p></p><p></p><p>up</p><p>down</p><div title="87% like this...">46<span><span>wbcarts
 at juno dot com</span></span> ?
<div title="2012-05-17 03:52">
<span>3 years ago</span><p></p>
<p><codefira mono code pro word-wrap:break-word display:block><span>BITWISE FLAGS for Custom
 PHP Objects<br>Sometimes I need a custom PHP Object that holds several boolean TRUE or FALSE values. I could easily include a variable for each of them, but as always, code has a way to get unweildy pretty fast. A more intelligent approach always seems to be the answer, even
 if it seems to be overkill at first.<br>I start with an abstract base class which will hold a single integer variable called $flags. This simple integer can hold 32 TRUE or FALSE boolean values. Another thing to consider is to just set certain BIT values without disturbing any of the other BITS --
 so included in the class definition is the setFlag($flag, $value) function, which will set only the chosen bit. Here's the abstract base class definition: <br><span><?php <br></span><span># BitwiseFlag.php<br></span><span>abstract class </span><span>BitwiseFlag<br></span><span>{<br>  protected </span><span>$flags</span><span>;<br>  </span><span>/*<br>   * Note: these functions are protected to prevent outside code<br>   * from falsely setting BITS. See how the extending class 'User'<br>   * handles this.<br>   *<br>   */<br>  </span><span>protected function </span><span>isFlagSet</span><span>(</span><span>$flag</span><span>)<br>  {<br>    return ((</span><span>$this</span><span>-></span><span>flags </span><span>& </span><span>$flag</span><span>)
 == </span><span>$flag</span><span>);<br>  }<br>  protected function </span><span>setFlag</span><span>(</span><span>$flag</span><span>, </span><span>$value</span><span>)<br>  {<br>    if(</span><span>$value</span><span>)<br>    {<br>      </span><span>$this</span><span>-></span><span>flags </span><span>|= </span><span>$flag</span><span>;<br>    }<br>    else<br>    {<br>      </span><span>$this</span><span>-></span><span>flags </span><span>&=
 ~</span><span>$flag</span><span>;<br>    }<br>  }<br>}<br></span><span>?><br></span><br>The class above is abstract and cannot be instantiated, so an extension is required. Below is a simple extension called User -- which is severely truncated for clarity. Notice I am defining const variables AND methods to use them.<br><span><?php <br></span><span># User.php<br></span><span>require(</span><span>'BitwiseFlag.php'</span><span>);<br>class </span><span>User </span><span>extends </span><span>BitwiseFlag<br></span><span>{<br>  const </span><span>FLAG_REGISTERED </span><span>= </span><span>1</span><span>; </span><span>//
 BIT #1 of $flags has the value 1<br>  </span><span>const </span><span>FLAG_ACTIVE </span><span>= </span><span>2</span><span>; 
    </span><span>// BIT #2 of $flags has the value 2<br>  </span><span>const </span><span>FLAG_MEMBER </span><span>= </span><span>4</span><span>; 
    </span><span>// BIT #3 of $flags has the value 4<br>  </span><span>const </span><span>FLAG_ADMIN </span><span>= </span><span>8</span><span>; 
     </span><span>// BIT #4 of $flags has the value 8<br>  </span><span>public function </span><span>isRegistered</span><span>(){<br>    return </span><span>$this</span><span>-></span><span>isFlagSet</span><span>(</span><span>self</span><span>::</span><span>FLAG_REGISTERED</span><span>);<br>  }<br>  public function </span><span>isActive</span><span>(){<br>    return </span><span>$this</span><span>-></span><span>isFlagSet</span><span>(</span><span>self</span><span>::</span><span>FLAG_ACTIVE</span><span>);<br>  }<br>  public function </span><span>isMember</span><span>(){<br>    return </span><span>$this</span><span>-></span><span>isFlagSet</span><span>(</span><span>self</span><span>::</span><span>FLAG_MEMBER</span><span>);<br>  }<br>  public function </span><span>isAdmin</span><span>(){<br>    return </span><span>$this</span><span>-></span><span>isFlagSet</span><span>(</span><span>self</span><span>::</span><span>FLAG_ADMIN</span><span>);<br>  }<br>  public function </span><span>setRegistered</span><span>(</span><span>$value</span><span>){<br>    </span><span>$this</span><span>-></span><span>setFlag</span><span>(</span><span>self</span><span>::</span><span>FLAG_REGISTERED</span><span>, </span><span>$value</span><span>);<br>  }<br>  public function </span><span>setActive</span><span>(</span><span>$value</span><span>){<br>    </span><span>$this</span><span>-></span><span>setFlag</span><span>(</span><span>self</span><span>::</span><span>FLAG_ACTIVE</span><span>, </span><span>$value</span><span>);<br>  }<br>  public function </span><span>setMember</span><span>(</span><span>$value</span><span>){<br>    </span><span>$this</span><span>-></span><span>setFlag</span><span>(</span><span>self</span><span>::</span><span>FLAG_MEMBER</span><span>, </span><span>$value</span><span>);<br>  }<br>  public function </span><span>setAdmin</span><span>(</span><span>$value</span><span>){<br>    </span><span>$this</span><span>-></span><span>setFlag</span><span>(</span><span>self</span><span>::</span><span>FLAG_ADMIN</span><span>, </span><span>$value</span><span>);<br>  }<br>  public function </span><span>__toString</span><span>(){<br>    return </span><span>'User [' </span><span>.<br>      (</span><span>$this</span><span>-></span><span>isRegistered</span><span>()
 ? </span><span>'REGISTERED' </span><span>: </span><span>''</span><span>)
 .<br>      (</span><span>$this</span><span>-></span><span>isActive</span><span>()
 ? </span><span>' ACTIVE' </span><span>: </span><span>''</span><span>)
 .<br>      (</span><span>$this</span><span>-></span><span>isMember</span><span>()
 ? </span><span>' MEMBER' </span><span>: </span><span>''</span><span>)
 .<br>      (</span><span>$this</span><span>-></span><span>isAdmin</span><span>()
 ? </span><span>' ADMIN' </span><span>: </span><span>''</span><span>)
 .<br>    </span><span>']'</span><span>;<br>  }<br>}<br></span><span>?><br></span><br>This seems like a lot of work, but we have addressed many issues, for example, using and maintaining the code is easy, and the getting and setting of flag values make sense. With the User class, you can now see how easy and intuitive bitwise flag operations
 become.<br><span><?php <br></span><span>require(</span><span>'User.php'</span><span>)<br></span><span>$user </span><span>= new </span><span>User</span><span>();<br></span><span>$user</span><span>-></span><span>setRegistered</span><span>(</span><span>true</span><span>);<br></span><span>$user</span><span>-></span><span>setActive</span><span>(</span><span>true</span><span>);<br></span><span>$user</span><span>-></span><span>setMember</span><span>(</span><span>true</span><span>);<br></span><span>$user</span><span>-></span><span>setAdmin</span><span>(</span><span>true</span><span>);<br>echo </span><span>$user</span><span>;  </span><span>//
 outputs: User [REGISTERED ACTIVE MEMBER ADMIN]<br></span><span>?></span></span></codefira></p>
<p></p>
<p></p>
<p>up</p>
<p>down</p>
<div title="100% like this...">6<span><span>zlel
 grxnslxves13 at hotmail dot com~=s/x/ee/g</span></span> ?
<div title="2005-10-26 07:30">
<span>9 years ago</span><p></p>
<p><codefira mono code pro word-wrap:break-word display:block><span>I refer to Eric Swanson's
 post on Perl VS PHP's implementation of xor. <br>Actually, this is not an issue with the implementation of XOR,  but a lot more to do with the lose-typing policy that PHP adopts. <br>Freely switching between int and float is good for most cases, but problems happen when your value is near the word size of your machine. Which is to say, 32-bit machines will encounter problems with values that hover around 0x80000000 - primarily because PHP
 does not support unsigned integers.<br>using bindec/decbin would address this issue as a work-around to do unsigned-int xor, but here's the real picture (i'm not claiming that this code will perform better, but this would be a better pedagogical code):<br><span><?php <br></span><span>function </span><span>unsigned_xor32 </span><span>(</span><span>$a</span><span>, </span><span>$b</span><span>) <br>{<br>        </span><span>$a1 </span><span>= </span><span>$a </span><span>& </span><span>0x7FFF0000</span><span>;<br>        </span><span>$a2 </span><span>= </span><span>$a </span><span>& </span><span>0x0000FFFF</span><span>;<br>        </span><span>$a3 </span><span>= </span><span>$a </span><span>& </span><span>0x80000000</span><span>;<br>        </span><span>$b1 </span><span>= </span><span>$b </span><span>& </span><span>0x7FFF0000</span><span>;<br>        </span><span>$b2 </span><span>= </span><span>$b </span><span>& </span><span>0x0000FFFF</span><span>;<br>        </span><span>$b3 </span><span>= </span><span>$b </span><span>& </span><span>0x80000000</span><span>;<br>        </span><span>$c </span><span>= (</span><span>$a3 </span><span>!= </span><span>$b3</span><span>)
 ? </span><span>0x80000000 </span><span>: </span><span>0</span><span>;<br>        return ((</span><span>$a1 </span><span>^ </span><span>$b1</span><span>)
 |(</span><span>$a2 </span><span>^ </span><span>$b2</span><span>))
 + </span><span>$c</span><span>;<br>}<br></span><span>$x </span><span>= </span><span>3851235679</span><span>;<br></span><span>$y </span><span>= </span><span>43814</span><span>;<br>echo </span><span>"<br>This is the value we want"</span><span>;<br>echo </span><span>"<br>3851262585"</span><span>;<br>echo </span><span>"<br>The result of a native xor operation on integer values is treated as a signed integer"</span><span>;<br>echo </span><span>"<br>"</span><span>.(</span><span>$x </span><span>^ </span><span>$y</span><span>);<br>echo </span><span>"<br>We therefore perform the MSB separately"</span><span>;<br>echo </span><span>"<br>"</span><span>.</span><span>unsigned_xor32</span><span>(</span><span>$x</span><span>, </span><span>$y</span><span>);<br></span><span>?><br></span><br>This is really foundation stuff, but for those of you who missed this in college, there seems to be something on 2's complement here: <br>http://www.evergreen.edu/biophysics/technotes/program/2s_comp.htm</span></codefira></p>
<p></p>
<p></p>
<p>up</p>
<p>down</p>
<div title="100% like this...">3<span><span>sag
 at ich dot net</span></span> ?
<div title="2013-07-14 06:52">
<span>2 years ago</span><p></p>
<p><codefira mono code pro word-wrap:break-word display:block><span>me reimplement for bitwise
 NOT (~)<br>    protected function flipBin($number) {<br>        $bin = str_pad(base_convert($number, 10, 2), 32, 0, STR_PAD_LEFT);<br>        for ($i = 0; $i             switch ($bin{$i}) {<br>                case '0' :<br>                    $bin{$i} = '1';<br>                    break;<br>                case '1' :<br>                    $bin{$i} = '0';<br>                    break;<br>            }<br>        }<br>        return bindec($bin);<br>    }<br>the benefit is, it works with numbers greater MAX_INT</span></codefira></p>
<p></p>
<p></p>
<p>up</p>
<p>down</p>
<div title="100% like this...">3<span><span>vivekanand
 dot pathak25 at gmail dot com</span></span> ?
<div title="2013-03-16 07:59">
<span>2 years ago</span><p></p>
<p><codefira mono code pro word-wrap:break-word display:block><span>$a =     9;<br>$b =     10;<br>echo $a & $b;<br>place value   128  64  32  16   8  4   2   1<br>$a                     0      0    0     0    1  0   0   1   =9<br>$b                     0      0    0     0    1   0  1   0   =10<br>result   8  <br>only bit they share together is the 8 bit. So 8 gets returned.<br>  $a =     36;<br>$b =     103;<br>echo $a & $b;<br>place value   128  64  32  16   8    4   2   1<br>$a                     0      0    1     0    0    1   0   0   =36<br>$b                     0      1    1     0    0    1   1   1   =103<br>result  32+4 = 36<br>the only bits these two share together are the bits 32 and 4 which when added together return 36.<br>$a =     9;<br>$b =     10;<br>echo $a | $b;<br>place value   128  64  32  16   8  4   2   1<br>$a                     0      0    0     0    1  0   0   1   =9<br>$b                     0      0    0     0    1   0  1   0   =10<br>result 8+2+1 = 11<br>3 bits set, in the 8, 2, and 1 column.add those up 8+2+1 and you get 11<br>$a =     9;<br>$b =     10;<br>echo $a ^ $b;<br>place value   128  64  32  16   8  4   2   1<br>$a                     0      0    0     0    1  0   0   1   =9<br>$b                     0      0    0     0    1   0  1   0   =10<br>result  2+1 = 3<br>the 2 bit and the 1 bit that they each have set but don't share. Soooo 2+1 = 3</span></codefira></p>
<p></p>
<p></p>
<p>up</p>
<p>down</p>
<div title="100% like this...">3<span><span>erich
 at seachawaii dot com</span></span> ?
<div title="2012-12-14 03:15">
<span>2 years ago</span><p></p>
<p><codefira mono code pro word-wrap:break-word display:block><span>Just a note regarding negative
 shift values, as the documentation states each shift is an integer multiply or divide (left or right respectively) by 2.  That means a negative shift value (the right hand operand) effects the sign of the shift and NOT the direction of the shift as I would
 have expected.  <br>FE. 0xff >> -2 results in 0x0 <br>and 0xff </span></codefira></p>
<p></p>
<p></p>
<p>up</p>
<p>down</p>
<div title="100% like this...">4<span><span>cw3theophilus
 at gmail dot com</span></span> ?
<div title="2009-04-15 02:22">
<span>6 years ago</span><p></p>
<p><codefira mono code pro word-wrap:break-word display:block><span>For those who are looking
 for a circular bit shift function in PHP (especially useful for cryptographic functions) that works with negtive values, here is a little function I wrote:<br>(Note: It took me almost a whole day to get this to work with negative $num values (I couldn't figure out why it sometimes worked and other times didn't), because PHP only has an arithmatic and not a logical bitwise right shift like I am used to. I.e. 0x80000001>>16
 will ouputs (in binary) "1111 1111 1111 1111 1000 0000 0000 0000" instead of "0000 0000 0000 0000 1000 0000 0000 0000" like you would expect. To fix this you have to apply the mask (by bitwise &) equal to 0x7FFFFFFF right shifted one less than the offset you
 are shifting by.)<br><span><?php <br></span><span>function </span><span>circular_shift</span><span>(</span><span>$num</span><span>,</span><span>$offset</span><span>)
 { </span><span>//Do a nondestructive circular bitwise shift, if offset positive shift left, if negative shift right<br>    </span><span>$num</span><span>=(int)</span><span>$num</span><span>;<br>    </span><span>$mask</span><span>=</span><span>0x7fffffff</span><span>; </span><span>//Mask
 to cater for the fact that PHP only does arithmatic right shifts and not logical i.e. PHP doesn't give expected output when right shifting negative values<br>    </span><span>if (</span><span>$offset</span><span>></span><span>0</span><span>)
 {<br>        </span><span>$num</span><span>=(</span><span>$num</span><span><span>$offset</span><span>%</span><span>32</span><span>)
 | ((</span><span>$num</span><span>>>(</span><span>32</span><span>-</span><span>$offset</span><span>%</span><span>32</span><span>))
 & (</span><span>$mask</span><span>>>(</span><span>31</span><span>-</span><span>$offset</span><span>%</span><span>32</span><span>)));<br>    }<br>    elseif (</span><span>$offset</span><span><span>0</span><span>){<br>        </span><span>$offset</span><span>=</span><span>abs</span><span>(</span><span>$offset</span><span>);<br>        </span><span>$num</span><span>=((</span><span>$num</span><span>>></span><span>$offset</span><span>%</span><span>32</span><span>)
 & (</span><span>$mask</span><span>>>(-</span><span>1</span><span>+</span><span>$offset</span><span>%</span><span>32</span><span>)))
 | (</span><span>$num</span><span><span>32</span><span>-</span><span>$offset</span><span>%</span><span>32</span><span>));<br>    }<br>    return </span><span>$num</span><span>; <br>}<br></span><span>?></span></span></span></span></span></codefira></p>
<p></p>
<p></p>
<p>up</p>
<p>down</p>
<div title="100% like this...">3<span><span>m0sh
 at hotmail dot com</span></span> ?
<div title="2008-08-07 12:03">
<span>7 years ago</span><p></p>
<p><codefira mono code pro word-wrap:break-word display:block><span>@greenone - nice function,
 thanks. I've adapted it for key usage:<br><span><?php <br></span><span>function </span><span>bitxor</span><span>(</span><span>$str</span><span>, </span><span>$key</span><span>)
 {<br>    </span><span>$xorWidth </span><span>= </span><span>PHP_INT_SIZE</span><span>*</span><span>8</span><span>;<br>    </span><span>// split<br>    </span><span>$o1 </span><span>= </span><span>str_split</span><span>(</span><span>$str</span><span>, </span><span>$xorWidth</span><span>);<br>    </span><span>$o2 </span><span>= </span><span>str_split</span><span>(</span><span>str_pad</span><span>(</span><span>''</span><span>, </span><span>strlen</span><span>(</span><span>$str</span><span>), </span><span>$key</span><span>), </span><span>$xorWidth</span><span>);<br>    </span><span>$res </span><span>= </span><span>''</span><span>;<br>    </span><span>$runs </span><span>= </span><span>count</span><span>(</span><span>$o1</span><span>);<br>    for(</span><span>$i</span><span>=</span><span>0</span><span>;</span><span>$i</span><span><span>$runs</span><span>;</span><span>$i</span><span>++)<br>        </span><span>$res </span><span>.= </span><span>decbin</span><span>(</span><span>bindec</span><span>(</span><span>$o1</span><span>[</span><span>$i</span><span>])
 ^ </span><span>bindec</span><span>(</span><span>$o2</span><span>[</span><span>$i</span><span>])); 
      <br>    return </span><span>$res</span><span>;<br>}<br></span><span>?></span></span></span></codefira></p>
<p></p>
<p></p>
<p>up</p>
<p>down</p>
<div title="100% like this...">3<span><span>Eric
 Swanson</span></span> ?
<div title="2005-08-31 05:19">
<span>9 years ago</span><p></p>
<p><codefira mono code pro word-wrap:break-word display:block><span>Perl vs. PHP implementation
 of the ^ operator:<br>After attempting to translate a Perl module into PHP, I realized that Perl's implementation of the ^ operator is different than the PHP implementation.  By default, Perl treats the variables as floats and PHP as integers.  I was able to verify the PHP use of
 the operator by stating "use integer;" within the Perl module, which output the exact same result as PHP was using.<br>The logical decision would be to cast every variable as (float) when using the ^ operator in PHP.  However, this will not yield the same results.  After about a half hour of banging my head against the wall, I discovered a gem and wrote a function using the
 binary-decimal conversions in PHP.<br>/*<br>not having much experience with bitwise operations, I cannot tell you that this is the BEST solution, but it certainly is a solution that finally works and always returns the EXACT same result Perl provides.<br>*/<br>function binxor($a, $b) {<br>    return bindec(decbin((float)$a ^ (float)$b));<br>}<br>//normal PHP code will not yeild the same result as Perl<br>$result = 3851235679 ^ 43814; //= -443704711<br>//to get the same result as Perl<br>$result = binxor(3851235679, 43814); //= 3851262585<br>//YIPPEE!!!<br>//to see the differences, try the following<br>$a = 3851235679 XOR 43814;<br>$b = 3851235679 ^ 43814; //integer result<br>$c = (float)3851235679 ^ (float)43814; //same as $b<br>$d = binxor(3851235679, 43814); //same as Perl!!<br>echo("A: $a<br>");<br>echo("B: $b<br>");<br>echo("C: $c<br>");<br>echo("D: $d<br>");</span></codefira></p>
<p></p>
<p></p>
<p>up</p>
<p>down</p>
<div title="91% like this...">10<span><span>grayda
 dot NOSPAM at DONTSPAM dot solidinc dot org</span></span> ?
<div title="2009-06-03 08:48">
<span>6 years ago</span><p></p>
<p><codefira mono code pro word-wrap:break-word display:block><span>Initially, I found bitmasking
 to be a confusing concept and found no use for it. So I've whipped up this code snippet in case anyone else is confused:<br><span><?php <br>    </span><span>// The various details a vehicle can have<br>    </span><span>$hasFourWheels </span><span>= </span><span>1</span><span>;<br>    </span><span>$hasTwoWheels  </span><span>= </span><span>2</span><span>;<br>    </span><span>$hasDoors      </span><span>= </span><span>4</span><span>;<br>    </span><span>$hasRedColour  </span><span>= </span><span>8</span><span>;<br>    </span><span>$bike          </span><span>= </span><span>$hasTwoWheels</span><span>;<br>    </span><span>$golfBuggy     </span><span>= </span><span>$hasFourWheels</span><span>;<br>    </span><span>$ford          </span><span>= </span><span>$hasFourWheels </span><span>| </span><span>$hasDoors</span><span>;<br>    </span><span>$ferrari       </span><span>= </span><span>$hasFourWheels </span><span>| </span><span>$hasDoors </span><span>| </span><span>$hasRedColour</span><span>;<br>    </span><span>$isBike        </span><span>= </span><span>$hasFourWheels </span><span>& </span><span>$bike</span><span>; </span><span>#
 False, because $bike doens't have four wheels<br>    </span><span>$isGolfBuggy   </span><span>= </span><span>$hasFourWheels </span><span>& </span><span>$golfBuggy</span><span>; </span><span>#
 True, because $golfBuggy has four wheels<br>    </span><span>$isFord        </span><span>= </span><span>$hasFourWheels </span><span>& </span><span>$ford</span><span>; </span><span>#
 True, because $ford $hasFourWheels<br></span><span>?><br></span><br>And you can apply this to a lot of things, for example, security:<br><span><?php <br>    </span><span>// Security permissions:<br>    </span><span>$writePost </span><span>= </span><span>1</span><span>;<br>    </span><span>$readPost </span><span>= </span><span>2</span><span>;<br>    </span><span>$deletePost </span><span>= </span><span>4</span><span>;<br>    </span><span>$addUser </span><span>= </span><span>8</span><span>;<br>    </span><span>$deleteUser </span><span>= </span><span>16</span><span>;<br>    <br>    </span><span>// User groups:<br>    </span><span>$administrator </span><span>= </span><span>$writePost </span><span>| </span><span>$readPosts </span><span>| </span><span>$deletePosts </span><span>| </span><span>$addUser </span><span>| </span><span>$deleteUser</span><span>;<br>    </span><span>$moderator </span><span>= </span><span>$readPost </span><span>| </span><span>$deletePost </span><span>| </span><span>$deleteUser</span><span>;<br>    </span><span>$writer </span><span>= </span><span>$writePost </span><span>| </span><span>$readPost</span><span>;<br>    </span><span>$guest </span><span>= </span><span>$readPost</span><span>;<br>    </span><span>// function to check for permission<br>    </span><span>function </span><span>checkPermission</span><span>(</span><span>$user</span><span>, </span><span>$permission</span><span>)
 {<br>        if(</span><span>$user </span><span>& </span><span>$permission</span><span>)
 {<br>            return </span><span>true</span><span>;<br>        } else {<br>            return </span><span>false</span><span>;<br>        }<br>    }<br>    </span><span>// Now we apply all of this!<br>    </span><span>if(</span><span>checkPermission</span><span>(</span><span>$administrator</span><span>, </span><span>$deleteUser</span><span>))
 {<br>        </span><span>deleteUser</span><span>(</span><span>"Some
 User"</span><span>); </span><span># This is executed because $administrator can $deleteUser<br>    </span><span>}<br></span><span>?><br></span><br>Once you get your head around it, it's VERY useful! Just remember to raise each value by the power of two to avoid problems</span></codefira></p>
<p></p>
<p></p>
<p>up</p>
<p>down</p>
<div title="87% like this...">6<span><span>ivoras
 at gmail dot com</span></span> ?
<div title="2011-06-06 04:44">
<span>4 years ago</span><p></p>
<p><codefira mono code pro word-wrap:break-word display:block><span>As an additional curiosity,
 for some reason the result of the operation ("18" & "32") is "10". In other words, try avoiding using the binary operators on strings :)</span></codefira></p>
<p></p>
<p></p>
<p>up</p>
<p>down</p>
<div title="85% like this...">10<span><span></span></span>?
<div title="2005-03-04 03:13">
<span>10 years ago</span><p></p>
<p><codefira mono code pro word-wrap:break-word display:block><span>A bitwise operators practical
 case :<br><span><?php <br>    </span><span>// We want to know the red, green and blue values of this color :<br>    </span><span>$color </span><span>= </span><span>0xFEA946 </span><span>;<br>    </span><span>$red </span><span>= </span><span>$color </span><span>>> </span><span>16 </span><span>;<br>    </span><span>$green </span><span>= (</span><span>$color </span><span>& </span><span>0x00FF00</span><span>)
 >> </span><span>8 </span><span>;<br>    </span><span>$blue </span><span>= </span><span>$color </span><span>& </span><span>0x0000FF </span><span>;<br>    </span><span>printf</span><span>(</span><span>'Red
 : %X (%d), Green : %X (%d), Blue : %X (%d)'</span><span>,<br>        </span><span>$red</span><span>, </span><span>$red</span><span>, </span><span>$green</span><span>, </span><span>$green</span><span>, </span><span>$blue</span><span>, </span><span>$blue</span><span>)
 ;<br>    </span><span>// Will display...<br>    // Red : FE (254), Green : A9 (169), Blue : 46 (70)<br></span><span>?></span></span></codefira></p>
<p></p>
<p></p>
<p>up</p>
<p>down</p>
<div title="83% like this...">4<span><span>zooly
 at globmi dot com</span></span> ?
<div title="2009-10-19 07:52">
<span>5 years ago</span><p></p>
<p><codefira mono code pro word-wrap:break-word display:block><span>Here is an example for bitwise
 leftrotate and rightrotate.<br>Note that this function works only with decimal numbers - other types can be converted with pack().<br><span><?php <br></span><span>function </span><span>rotate </span><span>( </span><span>$decimal</span><span>, </span><span>$bits</span><span>)
 {<br>  </span><span>$binary </span><span>= </span><span>decbin</span><span>(</span><span>$decimal</span><span>);<br>  return (<br>    </span><span>bindec</span><span>(</span><span>substr</span><span>(</span><span>$binary</span><span>, </span><span>$bits</span><span>).</span><span>substr</span><span>(</span><span>$binary</span><span>, </span><span>0</span><span>, </span><span>$bits</span><span>))<br>  );<br>}<br></span><span>// Rotate 124 (1111100) to the left with 1 bits<br></span><span>echo </span><span>rotate</span><span>(</span><span>124</span><span>, </span><span>1</span><span>);<br></span><span>// = 121 (1111001)<br>// Rotate 124 (1111100) to the right with 3 bits<br></span><span>echo </span><span>rotate</span><span>(</span><span>124</span><span>,
 -</span><span>3</span><span>);<br></span><span>// = 79 (1001111)<br></span><span>?></span></span></codefira></p>
<p></p>
<p></p>
<p>up</p>
<p>down</p>
<div title="80% like this...">3<span><span>josh
 at joshstrike dot com</span></span> ?
<div title="2011-02-11 07:54">
<span>4 years ago</span><p></p>
<p><codefira mono code pro word-wrap:break-word display:block><span>More referencing this for
 myself than anything... if you need to iterate through every possible binary combination where $n number of flags are set to 1 in a mask of $bits length: <br><span><?php  <br></span><span>echo </span><span>masksOf</span><span>(</span><span>3</span><span>,</span><span>10</span><span>); <br>function </span><span>masksOf</span><span>(</span><span>$n</span><span>,</span><span>$bits</span><span>)
 { <br>    </span><span>$u </span><span>= </span><span>pow</span><span>(</span><span>2</span><span>,</span><span>$bits</span><span>)-</span><span>1</span><span>; </span><span>//start
 value, full flags on. <br>    </span><span>$masks </span><span>= array(); <br>    while (</span><span>$u</span><span>></span><span>0</span><span>)
 { <br>        </span><span>$z </span><span>= </span><span>numflags</span><span>(</span><span>$u</span><span>); <br>        if (</span><span>$z</span><span>==</span><span>$n</span><span>) </span><span>array_push</span><span>(</span><span>$masks</span><span>,</span><span>$u</span><span>); <br>        </span><span>$u</span><span>--; <br>    } <br>    return (</span><span>$masks</span><span>);    <br>} <br>function </span><span>numflags</span><span>(</span><span>$n</span><span>)
 { <br>    </span><span>$k </span><span>= </span><span>0</span><span>; <br>    while (</span><span>$n</span><span>) { <br>        </span><span>$k </span><span>+= </span><span>$n </span><span>& </span><span>1</span><span>; <br>        </span><span>$n </span><span>= </span><span>$n </span><span>>> </span><span>1</span><span>; <br>    } <br>    return (</span><span>$k</span><span>); <br></span><span>//    alternately: <br>//    $u = 0; <br>//    for ($k=1;$k//        $u+=($n&$k?1:0); <br>//    } <br>//    return ($u); <br></span><span>} <br></span><span>?></span></span></codefira></p>
<p></p>
<p></p>
<p>up</p>
<p>down</p>
<div title="75% like this...">4<span><span>spencer-p-moy
 at example dot com</span></span> ?
<div title="2011-09-17 06:21">
<span>3 years ago</span><p></p>
<p><codefira mono code pro word-wrap:break-word display:block><span>The NOT or complement operator
 ( ~ ) and negative binary numbers can be confusing.<br>~2 = -3  because you use the formula  ~x = -x - 1  The bitwise complement of a decimal number is the negation of the number minus 1.<br>NOTE: just using 4 bits here for the examples below but in reality PHP uses 32 bits.<br>Converting a negative decimal number (ie: -3) into binary takes 3 steps:<br>1) convert the positive version of the decimal number into binary (ie: 3 = 0011)<br>2) flips the bits (ie: 0011 becomes 1100)<br>3) add 1 (ie: 1100  + 0001 = 1101)<br>You might be wondering how does 1101 = -3. Well PHP uses the method "2's complement" to render negative binary numbers. If the left most bit is a 1 then the binary number is negative and you flip the bits and add 1. If it is 0 then it is positive and you don't
 have to do anything. So 0010 would be a positive 2. If it is 1101, it is negative and you flip the bits to get 0010. Add 1 and you get 0011 which equals -3.</span></codefira></p>
<p></p>
<p></p>
<p>up</p>
<p>down</p>
<div title="75% like this...">4<span><span>Silver</span></span> ?
<div title="2009-05-12 04:36"><span>6 ye</span></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan