我需要将Javascript函数移植到C ++。在移植过程中,我找到了这行代码:
return (randseed[3]>>>0) / ((1 << 31)>>>0);
我的问题是:无符号右移零字节到右边意味着什么?移动零字节有什么意义?那相当于它的C / C ++会是什么?
这是我移植的功能:
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);
}
(资料来源:https://github.com/ethereum/blockies/blob/master/blockies.js)
这是我目前的端口:
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;
}
也许有C ++知识的人可能会检查我是否正确地做了这件事?
我还在函数内添加了变量的打印输出,期望右移零字节产生不同的结果
var before,after
before=randseed[3]
after=(randseed[3]>>>0)
console.log('before='+before+', after='+after)
return (randseed[3]>>>0) / ((1 << 31)>>>0);
但是console.log显示没有区别:
before=1458668765, after=1458668765
before=1818856068, after=1818856068
before=622943643, after=622943643