0

I need to port a Javascript function to C++. And during the porting process I found this line of code:

return (randseed[3]>>>0) / ((1 << 31)>>>0);

My question is: what does unsigned right shift zero bytes to the right means ??? What is the point of shifting zero bytes??? And what would be the equivalent to C/C++ of it?

This is the function I am porting:

    function rand() {
            // based on Java's String.hashCode(), expanded to 4 32bit values
            var t = randseed[0] ^ (randseed[0] << 11);

            randseed[0] = randseed[1];
            randseed[1] = randseed[2];
            randseed[2] = randseed[3];
            randseed[3] = (randseed[3] ^ (randseed[3] >> 19) ^ t ^ (t >> 8));

            return (randseed[3]>>>0) / ((1 << 31)>>>0);
    }

(Source: https://github.com/ethereum/blockies/blob/master/blockies.js)

And this is my current port:

double_t Accounts::rand(int *rand_seed) {
    int v1,v2;
    v1=rand_seed[0];
    v2=rand_seed[0] << 11;
    int t=v1 ^ v2;

    rand_seed[0]=rand_seed[1];
    rand_seed[1]=rand_seed[2];
    rand_seed[2]=rand_seed[3];
    v1=rand_seed[3] >> 19;
    v2=t>>8;
    rand_seed[3]=rand_seed[3] ^ v1 ^ t ^ v2;

    uint v=rand_seed[3];
    uint d=1<<31;

    double_t output=v / d;
    return output;
}

Maybe someone with C++ knowledge might check if I did it correcly?

I also added printouts of the variables inside the function, expecting right shift zero bytes produce different results

      var before,after
      before=randseed[3]     
      after=(randseed[3]>>>0)
      console.log('before='+before+', after='+after)
      return (randseed[3]>>>0) / ((1 << 31)>>>0);

But the console.log showed no difference:

before=1458668765, after=1458668765
before=1818856068, after=1818856068
before=622943643, after=622943643
3

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.