#37590 [Com]: Bit operators not responding equally

From: Date: Thu, 25 May 2006 18:58:30 +0000
Subject: #37590 [Com]: Bit operators not responding equally
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-97603@lists.php.net to get a copy of this message
 ID:               37590
 Comment by:       judas dot iscariote at gmail dot com
 Reported By:      jesse at eonstreet dot com
 Status:           Open
 Bug Type:         *Math Functions
 Operating System: Windows Xp / FC 3
 PHP Version:      5.1.4
 New Comment:

your first test returns "-4738672007" on Linux 64 bit. PHP_5_2 CVS.


Previous Comments:
------------------------------------------------------------------------

[2006-05-25 16:31:32] jesse at eonstreet dot com

Here is the code that had worked under 5.0.4 Windows / linux:

<?php

define('GOOGLE_MAGIC', 0xE6359A60);

	class googleTools{

		public static $url;

		public function __construct($url = false){



		}

		public function GoogleCH($url, $length=null, $init=GOOGLE_MAGIC) {
		    if(is_null($length)) {
		        $length = sizeof($url);
		    }
		    $a = $b = 0x9E3779B9;
		    $c = $init;
		    $k = 0;
		    $len = $length;
		    while($len >= 12) {
		        $a += ($url[$k+0] +($url[$k+1]<<8) +($url[$k+2]<<16)
+($url[$k+3]<<24));
		        $b += ($url[$k+4] +($url[$k+5]<<8) +($url[$k+6]<<16)
+($url[$k+7]<<24));
		        $c += ($url[$k+8] +($url[$k+9]<<8)
+($url[$k+10]<<16)+($url[$k+11]<<24));
		        $mix = googleTools::mix($a,$b,$c);
		        $a = $mix[0]; $b = $mix[1]; $c = $mix[2];
		        $k += 12;
		        $len -= 12;
		    }

		    $c += $length;
		    switch($len)              /* all the case statements fall through
*/
		    {
		        case 11: $c+=($url[$k+10]<<24);
		        case 10: $c+=($url[$k+9]<<16);
		        case 9 : $c+=($url[$k+8]<<8);
		          /* the first byte of c is reserved for the length */
		        case 8 : $b+=($url[$k+7]<<24);
		        case 7 : $b+=($url[$k+6]<<16);
		        case 6 : $b+=($url[$k+5]<<8);
		        case 5 : $b+=($url[$k+4]);
		        case 4 : $a+=($url[$k+3]<<24);
		        case 3 : $a+=($url[$k+2]<<16);
		        case 2 : $a+=($url[$k+1]<<8);
		        case 1 : $a+=($url[$k+0]);
		         /* case 0: nothing left to add */
		    }
		    $mix = googleTools::mix($a,$b,$c);
		    /*-------------------------------------------- report the result
*/

		    return $mix[2];
		}

		public function doGetRank($url) {




		   	$url = 'info:'.$url;
		   	$GoogleCH = googleTools::GoogleCH(googleTools::strord($url));
				$url = urlencode($url);
		    $file =
"http://www.google.com/search?client=navclient-auto&ch=6$GoogleCH&features=Rank&q=$url";
		    //$data = file($file);

		    $ch = curl_init();    // initialize curl handle
				curl_setopt($ch, CURLOPT_URL,$file); // set url to post to
				curl_setopt($ch, CURLOPT_FAILONERROR, 1);
				curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
				curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a
variable
				curl_setopt($ch, CURLOPT_TIMEOUT, 25); // times out after 4s
				$data = curl_exec($ch); // run the whole process
				curl_close($ch);
		    $rankarray = explode (':', $data);
		    $rank = $rankarray[2];

		    return $rank;
		}

		//converts a string into an array of integers containing the numeric
value of the char
		private function strord($string) {
		    for($i=0;$i<strlen($string);$i++) {
		        $result[$i] = ord($string{$i});
		        $strResult .= $i ."-".$result[$i];
		    }
//		    echo "<br>".$strResult."<br>";
		    return $result;


		}

		//unsigned shift right
		private function zeroFill($a, $b)
		{
			$aTemp =$a;
		    $z = hexdec(80000000);
		        if ($z & $a)
		        {
		            $a = ($a>>1);
		            $a &= (~$z);
		            $a |= 0x40000000;
		            $a = ($a>>($b-1));
		        }
		        else
		        {
		            $a = ($a>>$b);
		        }

		        return $a;


		}

		private function mix($a,$b,$c) {
		  $a -= $b; $a -= $c; $aTemp = $a; $a ^=
(googleTools::zeroFill($c,13));
		  $b -= $c; $b -= $a; $b ^= ($a<<8);
		  $c -= $a; $c -= $b; $c ^= (googleTools::zeroFill($b,13));
		  $a -= $b; $a -= $c; $a ^= (googleTools::zeroFill($c,12));
		  $b -= $c; $b -= $a; $b ^= ($a<<16);
		  $c -= $a; $c -= $b; $c ^= (googleTools::zeroFill($b,5));
		  $a -= $b; $a -= $c; $a ^= (googleTools::zeroFill($c,3));
		  $b -= $c; $b -= $a; $b ^= ($a<<10);
		  $c -= $a; $c -= $b; $c ^= (googleTools::zeroFill($b,15));

		  $arr = array($a,$b,$c);
		  print_a($arr);

		  return $arr;
		}

	}

?>

------------------------------------------------------------------------

[2006-05-25 13:28:30] jesse at eonstreet dot com

Yes, they are dependent to a point.  But in 5.0.4 they returned the
same end value.

Something has changed in 5.1.x that no longer produces the correct
value.

------------------------------------------------------------------------

[2006-05-25 13:17:54] tony2001@php.net

Bitwise operators are architecture dependent.
And -4738698913 is actually not an integer, but a float on 32bit
platform.

------------------------------------------------------------------------

[2006-05-25 13:08:07] jesse at eonstreet dot com

Description:
------------
** Sorry if I havfe put this in hte wrong category.

I have encountered a difference in the output of a bit operator
combination between windows and FC3.  

The example below is part of the code that produces the Google Checksum
when submitting to get a page rank. 

 In Mozilla Javascript and windows Javascript and well as PHP 5.1.4 on
whindows XP the bit operation returns correclty but under linux there
is a difference

I don't no why.

Reproduce code:
---------------
<?

$a = -4738698913;
echo $a ^= 43814;  

?>

Expected result:
----------------
In Windows the result is -443704711
In Linux FC3 the result is -2147439834



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=37590&edit=1


Thread (8 messages)

« previous php.bugs (#97603) next »