Flipping bits

HackerRank 問題: Flipping bits

Question

You will be given a list of 32 bit unsigned integers. Flip all the bits ( 1 -> 0 and 0 -> 1) and return the result as an unsigned integer.

HackerRank 問題: Flipping bits

Answer

<?php

/*
 * Complete the 'flippingBits' function below.
 *
 * The function is expected to return a LONG_INTEGER.
 * The function accepts LONG_INTEGER n as parameter.
 */

function flippingBits($decimal_number)
{
    // max decimal number 4294967295
    $max_decimal_number = pow(2 , 32) - 1;
    // minus decimal number
    $minus_decimal_number = $max_decimal_number - $decimal_number;

    return $minus_decimal_number;
}

$fptr = fopen(getenv("OUTPUT_PATH"), "w");

$q = intval(trim(fgets(STDIN)));

for ($q_itr = 0; $q_itr < $q; $q_itr++) {
    $n = intval(trim(fgets(STDIN)));

    $result = flippingBits($n);

    fwrite($fptr, $result . "\n");
}

fclose($fptr);

Reference