PHP7按位数按位移位将在PHP中引发ArithmeticError

We're looking to switch to a PHP7 server and are running some compatibility checks on existing code of a website. One of the problems found is with the following function and the Bitwise shifts by a negative number.

Apparently this will throw errors in PHP7. I'll admit I don't fully understand how this arithmetic works, but I was wondering if anyone had a suggesting on how to modify the function to #1 maintain the functionality and #2 become PHP7 compliant.

/**
 * Right shift with zero fill.
 *
 * @param integer $a number to shift
 * @param integer $b number of bits to shift
 * @return integer
 */
public function zeroFill($a, $b){
    return ($a >= 0) ? ($a >> $b) : ($a >> $b) & (PHP_INT_MAX >> ($b - 1));
}

The ArithmeticError is thrown when an mathematical calculation doesn't happen as normally expected, which in this case could lead to values outside of the possible bounds of an integer. (See documentation here)

Your function is already validating this and limiting the operation for a PHP_INT_MAX, so there is no problem there.

As to remove the alert message thrown by your verification plugin, you need to cover the function with a try/catch block in order to capture a possible ArithmeticError. This way the function would be like:

public function zeroFill($a, $b){
    try {
        return ($a >= 0) ? ($a >> $b) : ($a >> $b) & (PHP_INT_MAX >> ($b - 1));
    } catch(ArithmeticError $error) {
        // Do your logic to treat this kind of error
    }
    return $a; // Or return something you would want
}

Also, using the str_pad is always a good alternative.