Skip to content

Instantly share code, notes, and snippets.

@thachp
Last active August 29, 2015 14:23
Show Gist options
  • Save thachp/e83a6bf96e01c0a83249 to your computer and use it in GitHub Desktop.
Save thachp/e83a6bf96e01c0a83249 to your computer and use it in GitHub Desktop.
Arithmetic Operations without using the operations
<?php
/**
* Add number without using any arithmetic operators.
* @author thachp *
**/
// add & subject
function add($a, $b) {
if ($b === 0) return $a;
$sum = $a ^ $b; // add
$carry = ($a & $b) << 1; // add without carry
//echo("sum:" . $sum . ' - '. decbin($sum) . ", " . "carry:" . $carry . " - " . decbin($carry) . "\n \n");
return add($sum, $carry);
};
echo("<pre>");
//echo("5,3 : " . decbin(5) . " + " . decbin(3) . "\n");
echo (add(5,3));
echo("\n");
echo (add(5,-3));
echo("</pre>");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment