note 40010 deleted from function.gmp-xor by nlopess
| From: | nlopess@php.net | Date: | Thu, 19 Feb 2004 13:42:34 +0000 |
| Subject: | note 40010 deleted from function.gmp-xor by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-65394@lists.php.net to get a copy of this message | ||
Note Submitter: Cennet
----
Quite annoying that the gmp_xor feature is not implemented on windows. To resolve this shortcomming,
I have produced 2 helper functions:
function int_xor ($val1, $val2) {
$res = 0;
for($i=0; $i<32; ++$i)
$res |= (($val1 & (1 << $i)) xor ($val2 & (1 << $i))) << $i;
return $res;
}
function str_xor ($val1, $val2) {
$res = "";
for ($i=0; $i<strlen($val1); ++$i)
$res .= ($val1[$i] == $val2[$i]) ? "0":"1";
return $res;
}
The first one works for 32 bit integers, the second one works for strings, ea "0010011".
You could use the string version with integers like:
str_xor(sprintf("%032b", number1), sprintf("%032b", number2));
But since the int version uses bit shifting, it is supposedly faster (depending on implementation of
the shifting operator).
You should probally note that the string version assumes that both strings are of equal length (or
at least that the shortest string is passed as the first argument). Also, no type checking is
performed.