As the challenge we’ve given you last week was a real hit, we want to provoke you again!
So… you are given 2 integral numbers: a and b.
Request: Write a code sequence that interchanges the values of these 2, without using additional variables.
As the challenge we’ve given you last week was a real hit, we want to provoke you again!
So… you are given 2 integral numbers: a and b.
Request: Write a code sequence that interchanges the values of these 2, without using additional variables.
$a= $b-$a;
$b=$a-$b;
$a=$a+$b;
$a ^= $b ^= $a ^= $b;
Well done, guys!
Our solutions were:
1.
$a = $a + $b
$b = $a – $b
$a = $a – $b
2.
$a = $a * $b;
$b = $a / $b;
$a = $a / $b;
3.
$a = $a ^ $b;
$b = $a ^ $b;
$a = $a ^ $b;
4.
list($a, $b) = array($b, $a);
OR
list($a,$b) = array_reverse(array($a,$b));
5.
extract(array(‘a’=>$b, ‘b’=>$a));
6.
file_put_contents(‘a.txt’,$a);
file_put_contents(‘b.txt’,$b);
$a = file_get_contents(‘b.txt’);
$b = file_get_contents(‘a.txt’);
7.
$a = $a <> strlen(decbin($b));
$a = $b << (strlen(decbin($a)) – strlen(decbin($b))) ^ $a;
So.. when it comes to web development, we do get creative!