Jump to content

Bit Wise JavaScript and PHP

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
MrGamma

MrGamma

    Learning Programmer

  • Members
  • PipPipPip
  • 50 posts
I'm having an issue trying to figure out why in javascript.

1532515508 ^ -3955803181 = 1332703079

but in php it equals...

1532515508 ^ -3955803181 = -614968140

Does anybody know why?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It depends on the number of bits used to represent those values. If they are using a different number of bits, you will get different results.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
MrGamma

MrGamma

    Learning Programmer

  • Members
  • PipPipPip
  • 50 posts
Never mind... JavaScript was using unsigned integers while Php was using Signed...

At least I think that's what was happening... This fixed it...

function _unSign($i1) {
    if(0x7fffffff < $i1) {
        $i1 -= 0xffffffff + 1.0;
    }elseif (-0x80000000 > $i1) {
        $i1 += 0xffffffff + 1.0;
    }
    return $i1;
}

_unSign(1532515508) ^ _unSign(-3955803181) = 1332703079