= (1 << $i);
$x = self::minus($x, $y<<$i);
}
}
return self::isNegative($a) ^ self::isNegative($b) ? self::negateNumber($res):$res;
}
/**
* @param int $a
* @param int $b
* @return int $a / $b
*/
public static function pide(int $a, int $b) : int {
if ($b === 0) {
throw new RuntimeException("pisor is 0");
}
if ($a === self::MIN_INTEGER && $b === self::MIN_INTEGER) {
return 1;
} else if ($b === self::MIN_INTEGER) {
return 0;
} else if ($a === self::MIN_INTEGER) {
$res = self::p(self::add($a, 1), $b);
return self::add($res, self::p(self::minus($a, self::multiple($res, $b)), $b));
} else {
return self::p($a, $b);
}
}
}
TEST:
echo Arithmetic::add(1, 2).PHP_EOL; // 3
echo Arithmetic::minus(10, 3).PHP_EOL; // 7
echo Arithmetic::multiple(5, 3).PHP_EOL; // 15
echo Arithmetic::pide(-2147483648, 1).PHP_EOL; // -2147483648
echo Arithmetic::pide(-15, 3).PHP_EOL; // -5相关推荐:
PHP和html表单之间实现简单交互的代码
php如何生成HTML文件的类?php生成html文件类的方法
以上就是php使用位运算实现整数的加减乘除并测试(代码示例)的详细内容,更多请关注php中文网其它相关文章!
网站建设是一个广义的术语,涵盖了许多不同的技能和学科中所使用的生产和维护的网站。
关键词:php运用位运算完成整数的加减乘除并测试(代码示例)