Re: while-question

From: Date: Mon, 17 Nov 2008 15:32:02 +0000
Subject: Re: while-question
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-283363@lists.php.net to get a copy of this message
Stut wrote:
On 17 Nov 2008, at 14:31, Nathan Rixham wrote:
if you really want a challenge try this one.. task: add the numbers 5 and 17 together, using php, without using the + operator. fill it in: function add($a , $b) { //code here but no + - / * operators return $answer; } echo add(5, 17);
Elemental my dear Mr Rixham... function add($a , $b) { $answer = $a ^ $b; while (0 != ($a & $b)) {
    $b = ($a & $b) << 1;
    $answer = $answer ^ $b;
} return $answer; } -Stut
what an answer; very nice and indeed full marks mr stut! now then.. echo add(5,17) . ' ' . (5 + 17) . PHP_EOL; echo add(-5,17) . ' ' . (-5 + 17) . PHP_EOL; echo add(5,-17) . ' ' . (5 + -17) . PHP_EOL; echo add(-5,-17) . ' ' . (-5 + -17) . PHP_EOL; (can you tell I've been down this route before?)

« previous php.general (#283363) next »