How to use 64 bit in PHP

80 Views Asked by At

I wrote a function which will output a sequence of gray codes. For an input of N, showing the last N gray code numbers of N-bit.

Now, this function is not giving the correct output if the input goes over 31. But I need this to work for at least 64. How can I achieve this?

Here is the function:

function gray_encode($binary) {
    $powered = pow(2, $binary) - 1;

    for ( $i = ($powered - $binary)+1; $i < ($powered+1); $i++) {
        echo decbin($i ^ ($i >> 1))  . PHP_EOL;
    }
}

gray_encode(31);

Right Output for 31: http://codepad.org/8LotGD35

Wrong Output for 33: http://codepad.org/JmfIOrrj

Full output for 1-31: http://codepad.viper-7.com/aDPx68

0

There are 0 best solutions below