mercredi 18 juin 2014

maximum gap between two 1s in a binary equivalent of a decimal number


Vote count:

0




I want to write a program to find the maximum gap between two 1s in a binary equivalent of a decimal number. For example for 100101: the gap is 2 and for 10101: the gap is 1.



<?php
$numberGiven = 251;
$binaryForm = decbin($numberGiven);

$status = false;
$count = 0;
for($i = 0; $i < strlen($binaryForm); $i++)
{
var_dump($binaryForm[$i]);
if($binaryForm[$i] == 1)
{
$status = false;
$count = 0;
}
else
{
$status = true;
$count += 1;
}
}
echo "count = " . $count . "<br>";
echo $binaryForm;
?>


but i was not successfull..



asked 24 secs ago






Aucun commentaire:

Enregistrer un commentaire