<?php
//判断奇偶 if (($a & 1) == 0)代替if ($a % 2 == 0)来判断$a是不是偶数
for ($i = 0; $i < 100; ++$i)
if ($i & 1)
echo $i.' ';
echo '<hr/>';
//交换两数 异或运算的两个特性——1.自己与自己异或结果为0,2.异或满足交换律
$a = 1; $b = 2;
$a ^= $b;
$b ^= $a;
$a ^= $b;
echo $a.' '.$b;
echo '<hr/>';
//变换符号
$a = 2;$b=-3;
$a=~$a + 1;$b=~$b + 1;
echo $a.' '.$b;
echo '<hr/>';
//求绝对值
$a = -3;
//$i = $a >> 31;
//echo $i == 0 ? $a : (~$a + 1);
$i = $a >> 31;
echo (($a ^ $i) - $i);
echo '<hr/>';
//不用加减乘除做加法
function add_func($a,$b){
$sum = 0; $carry = 0;
do{
$sum = $a ^ $b;
$carry = ($a&$b)<<1;
$a = $sum ;
$b = $carry;
}while($b!=0);
echo $a;
}
add_func(12,33);
http://blog.csdn.net/morewindows/article/details/7354571