Valhalla Legends Forums Archive | Web Development | PHP vs JS (XOR problem)

AuthorMessageTime
Barabajagal
Trying to convert a javascript file to php, I've run into a bit of a problem.
[code]<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>";
?>[/code]
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?
November 13, 2007, 8:37 AM
St0rm.iD
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
November 14, 2007, 6:47 PM
Barabajagal
I just need PHP to mimic JS. How can I tell PHP to handle it as...whatever JS handles it as?
November 14, 2007, 8:39 PM
Leaky
pack
November 15, 2007, 1:34 AM
St0rm.iD
yes, that might work.
November 15, 2007, 7:05 PM
Barabajagal
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...
November 15, 2007, 9:31 PM
iago
Apparently, the script listed here can do that:
http://talk.iwebtool.com/thread2082.html

*shrug*
November 15, 2007, 9:42 PM
Barabajagal
Doesn't work correctly on my server...  It's the same code I had converted from javascript, but without the URL behind it.
November 15, 2007, 10:39 PM

Search