Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript中模拟64位(无符号)整数的逐位旋转?_Javascript_Bit Manipulation - Fatal编程技术网

如何在JavaScript中模拟64位(无符号)整数的逐位旋转?

如何在JavaScript中模拟64位(无符号)整数的逐位旋转?,javascript,bit-manipulation,Javascript,Bit Manipulation,我需要在JavaScript中执行64位整数的循环左移位。然而: JavaScript数字是双倍的 当您开始使用>和>>>以及~以及所有的位旋转业务时,JavaScript将它们转换为32位有符号整数。然后当你完成后,又回到双打。我想 我不要这个标志。我绝对不想要小数点。但我确实想要64位 那么,如何执行64位值的按位左旋转呢?我相信是这样,尽管不是最有效的方法,但可以将数字转换为二进制形式的字符串(64位),使用子字符串将字符移动到开头并附加到结尾(用于左旋转),然后将二进制形式转换回数字

我需要在JavaScript中执行64位整数的循环左移位。然而:

  • JavaScript数字是双倍的
  • 当您开始使用>和>>>以及~以及所有的位旋转业务时,JavaScript将它们转换为32位有符号整数。然后当你完成后,又回到双打。我想
  • 我不要这个标志。我绝对不想要小数点。但我确实想要64位

那么,如何执行64位值的按位左旋转呢?

我相信是这样,尽管不是最有效的方法,但可以将数字转换为二进制形式的字符串(64位),使用子字符串将字符移动到开头并附加到结尾(用于左旋转),然后将二进制形式转换回数字。我相信您可以找到如何将十进制数转换为二进制形式并转换为字符串。

我认为唯一可以做到的方法是创建一个int64类,该类内部包含两个32位整数,并通过在它们之间进行移位。

将您的64位数字保持为单独的高分区和低分区。若要在N<32时向左旋转N:

hi_rot=((hi>>(32-N))和(0xffffff)

lo_rot=((lo>>(32-N)))和(0xffffff)


如果N>=32,则从N中减去32,交换hi和lo,然后执行上述操作。

这是一个基于值的旋转

double d = 12345678901.0;
// get high int bits in hi, and the low in
int hi = (int)(d / 16.0 / 16.0 / 16.0 / 16.0);
int low = (int)d;

int rot = 3; // thus * 8
int newhi = (low >> (32 - rot)) | (hi << rot);
int newlow = (hi >> (32 - rot)) | (low << rot);

double newdouble = ((double)hi * 16.0 * 16.0 * 16.0 * 16.0) + (double)low;
double d=12345678901.0;
//在hi中获取高int位,在hi中获取低int位
int hi=(int)(d/16.0/16.0/16.0/16.0);
int低=(int)d;
int rot=3;//因此*8

int newhi=(low>>(32-rot))|(hi>(32-rot))|(low正如@Doug Currie所说的,您需要将64位数字表示为两个数字,然后对它们执行逐位操作。我使用的代码是:

//Constructor for a Long..
function Long(high, low) {
    //note: doing "or 0", truncates to 32 bit signed
    //big-endian 2's complement int..
    this._high = high | 0;
    this._low = low | 0;
}
Long.prototype.rotateLeft = function(bits) {
    var newHigh;
     if(bits === 32){ //just switch high and low over in this case..
        newHigh = this._low;
        this._low = this._high;
        this._high = newHigh;
    } else {
        newHigh = (this._high << bits) | (this._low >>> (32-bits)); 
        this._low = (this._low << bits) | (this._high >>> (32-bits));
        this._high = newHigh;
    }
    return this; //for chaining..
};
//Rotates the bits of this word round to the right (max 32)..
Long.prototype.rotateRight = function(bits) {
    var newHigh;
    if(bits === 32){ //just switch high and low over in this case..
        newHigh = this._low;
        this._low = this._high;
        this._high = newHigh;
    } else {
        newHigh = (this._low << (32-bits)) | (this._high >>> bits); 
        this._low = (this._high << (32-bits)) | (this._low >>> bits);
        this._high = newHigh;
    }
    return this; //for chaining..
};
//长时间的构造函数。。
功能长(高、低){
//注意:执行“或0”将截断为32位有符号
//大端2的补码int。。
这个._high=high | 0;
这个。_低=低| 0;
}
Long.prototype.rotateLeft=函数(位){
var newHigh;
如果(位===32){//在这种情况下只需切换高电平和低电平。。
newHigh=这个;
这个。_低=这个。_高;
这个。_high=newHigh;
}否则{
newHigh=(此._high>>(32位));
this._low=(this._low>>(32位));
这个。_high=newHigh;
}
返回此;//用于链接。。
};
//将此字的位向右旋转(最大32位)。。
Long.prototype.rotateRight=函数(位){
var newHigh;
如果(位===32){//在这种情况下只需切换高电平和低电平。。
newHigh=这个;
这个。_低=这个。_高;
这个。_high=newHigh;
}否则{
newHigh=(此._low>>位);
此。_低=(此。_高>>位);
这个。_high=newHigh;
}
返回此;//用于链接。。
};

要使用它,请尝试运行:
console.log(新的Long(0,1).rotateLeft(4))
然后检查_high和_low属性。

你确定你的Javascript将始终在64位平台上运行吗?p.s.对任何告诉我不要在Javascript中这样做的人都投反对票。没有帮助!我知道这不是Javascript非常适合的任务。但我无论如何都需要这样做。KTHXBAI.no我不相信你,但是如果JavaScript将它存储在一个双精度中,那么它就是一个双精度而不是一个64位的int(不考虑符号)。叹气。这就是我在问题中使用“模拟”这个词的原因。我不需要JavaScript将它存储为一个64位的int;我只需要做一些事情,在我说的时候给出正确的答案“循环左移此值35位,然后告诉我如果使用64位整数,结果的整数值是多少。"我想旋转double表示的整数。对不起,也许我的问题不清楚;我想旋转一个64位无符号整数。我研究了JavaScript的数字内部表示,因为我认为JavaScript中浮点和整数之间缺乏区别,这会使我更难做我想做的事情。不,这是一个合法的解决方案,即使它不是我所希望的那种解决方案。没有对你的否决票。这是一个C版本,因为带符号的int的最后一步实际上并没有像预期的那样工作。事实上,它很可能也不适用于负双倍以太…使用算术运算速度太慢,特别是当你ant转换大量字节,例如图像数据,这不是一个实用的解决方案。我喜欢这种方法,但数字有符号会导致任何错误吗?最重要的位存储符号。哦,你说的小于32。看起来这解决了问题。唯一的问题是
&(0xFFFFFF)
在这里是不可操作的。如果要使数字无符号,请使用
>>0