note 40010 added to function.gmp-xor

From: Date: Wed, 18 Feb 2004 21:54:48 +0000
Subject: note 40010 added to function.gmp-xor
Groups: php.notes 
Request: Send a blank email to php-notes+get-65355@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/function.gmp-xor.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40010 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40010&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40010&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65355) next »