Valhalla Legends Archive

Programming => Web Development => Topic started by: Barabajagal on November 13, 2007, 02:37 AM

Title: PHP vs JS (XOR problem)
Post by: Barabajagal on November 13, 2007, 02:37 AM
Trying to convert a javascript file to php, I've run into a bit of a problem.
<script type="text/javascript">
function TryXOR()
{
  var a = -5664999325;
  var b = 195920;
  a ^= b;
  document.write(a);
}
TryXOR();
</script>
<?php $a = -5664999325; $b = 195920; $a ^= $b; echo "<br>$a<br>";?>

In JS, I get a result of -1370225357. This is the result I want. In PHP, though, I get -2147287728. Anyone care to help me out?

Edit: It appears that if I set 'a' in PHP to 5664999325, it returns 1370225357. I'm confused. Why does JS ignore the signed value and then re-apply it?

Edit #2: Using -915899570 for 'a' and 345042 for 'b' returns -915588964 for JS (and VB, and normal php XOR), and -915588960 for my modified PHP xor. This is just driving me mad. What the hell's wrong with PHP?
Title: Re: PHP vs JS (XOR problem)
Post by: Banana fanna fo fanna on November 14, 2007, 12:47 PM
Bitwise xor relies pretty heavily on the size of the datatype you are using as well as its numeric representation. I'm pretty sure - though not certain, as I'm really tired - that your problem has to do with with one of those two things. In short, unsigned/signed problem
Title: Re: PHP vs JS (XOR problem)
Post by: Barabajagal on November 14, 2007, 02:39 PM
I just need PHP to mimic JS. How can I tell PHP to handle it as...whatever JS handles it as?
Title: Re: PHP vs JS (XOR problem)
Post by: Leaky on November 14, 2007, 07:34 PM
pack
Title: Re: PHP vs JS (XOR problem)
Post by: Banana fanna fo fanna on November 15, 2007, 01:05 PM
yes, that might work.
Title: Re: PHP vs JS (XOR problem)
Post by: Barabajagal on November 15, 2007, 03:31 PM
Doesn't matter, I gave up on the project. If anyone was wondering, it was going to be a php script to display the Google page rank of any page you gave it...
Title: Re: PHP vs JS (XOR problem)
Post by: iago on November 15, 2007, 03:42 PM
Apparently, the script listed here can do that:
http://talk.iwebtool.com/thread2082.html

*shrug*
Title: Re: PHP vs JS (XOR problem)
Post by: Barabajagal on November 15, 2007, 04:39 PM
Doesn't work correctly on my server...  It's the same code I had converted from javascript, but without the URL behind it.