Sidebars examples

Sidebar

Bitwise.php

Code


<?php

function showBinaryFormatOfInteger(int $number) : string {

    $c = 1;
    $returnString = 'Binary format of integer ' . $number . ":<br />";

    while ($number) {
        $returnString .= $c . ' : ' . ($number & 1) . "<br />";
        
        $c <<= 1;

        $number >>= 1;
    }

    return $returnString;

}

echo showBinaryFormatOfInteger(987);


?>
Output
Binary format of integer 987:
1 : 1
2 : 1
4 : 0
8 : 1
16 : 1
32 : 0
64 : 1
128 : 1
256 : 1
512 : 1