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