• Welcome to Valhalla Legends Archive.
 

PHP vs JS (XOR problem)

Started by Barabajagal, November 13, 2007, 02:37 AM

Previous topic - Next topic

Barabajagal

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?

Banana fanna fo fanna

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

Barabajagal

I just need PHP to mimic JS. How can I tell PHP to handle it as...whatever JS handles it as?


Banana fanna fo fanna


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...

iago

This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Barabajagal

Doesn't work correctly on my server...  It's the same code I had converted from javascript, but without the URL behind it.