Count Set Bits
PHP
Medium
5 views
Problem Description
Print how many 1-bits are in n.
Input Format
One integer n.
Output Format
One integer count.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$n=intval($inputText);
$c=0;
while($n>0){
$c += ($n & 1);
$n >>= 1;
}
echo $c;
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!