Multiply Without Star
PHP
Hard
5 views
Problem Description
Multiply a and b using only addition and bit shifts.
Input Format
One line: a b (non-negative).
Output Format
One integer product.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
[$a,$b]=array_map('intval',preg_split('/\\s+/', $inputText));
$res=0;
while($b>0){
if($b&1) $res += $a;
$a <<= 1;
$b >>= 1;
}
echo $res;
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!