特别注意php中位运算需要加括号。
<?php
class Solution {
function countOdds($low, $high) {
return (($high + 1) >> 1) - ($low >> 1);
}
}
//var_dump(7 >> 1);exit;
$s = new Solution();
$result = $s->countOdds(3, 7);
assert($result == 3, 'low:3, high:7, result should be 3!');
$result = $s->countOdds(8, 10);
assert($result == 1, 'low:8, high:10, result should be 1!');
$result = $s->countOdds(407656828,722359384); // 157351278
assert($result == 157351278, 'low:407656828, high:722359384, result should be 157351278!');
// 3 7 => 3
// 3 10 => 4
// 2 7 => 3
// 2 10 => 4
// 4 16 => 6