php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37590 Bit operators not responding equally
Submitted: 2006-05-25 13:08 UTC Modified: 2006-05-26 13:51 UTC
From: jesse at eonstreet dot com Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 5.1.4 OS: Windows Xp / FC 3
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jesse at eonstreet dot com
New email:
PHP Version: OS:

 

 [2006-05-25 13:08 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-05-25 13:17 UTC] 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:28 UTC] 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 16:31 UTC] 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 18:58 UTC] judas dot iscariote at gmail dot com
your first test returns "-4738672007" on Linux 64 bit. PHP_5_2 CVS.
 [2006-05-26 06:39 UTC] tony2001@php.net
>Something has changed in 5.1.x that no longer produces the correct value.
Your server has changed.
 [2006-05-26 13:48 UTC] jesse at eonstreet dot com
Confirmed.  I have just up graded on a FC2 machine from 5.0.4 to 5.1.4 and the result is correct.  Well, back to the investigation as to why every server but my production server gives the correct value :(
 [2006-05-26 13:51 UTC] jesse at eonstreet dot com
Is there a specific extension that governs this type of math functions? Maybe a library needs updating or I  didn't enable it when I compiled.

Thanks for your help.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Oct 29 07:01:24 2020 UTC