English 中文(简体)
How do I simulate a bitwise rotation of a 64-bit (unsigned) integer in JavaScript?
原标题:

I need to perform a circular left shift of a 64-bit integer in JavaScript. However:

  • JavaScript numbers are doubles
  • JavaScript converts them to 32-bit signed ints when you start with the << and the >> and the >>> and the ~ and all of the bit-twiddling business. And then it s back to doubles when you re done. I think.
  • I don t want the sign. And I definitely don t want the decimal bits. But I definitely do want 64 bits.

So, how do I perform a bitwise left rotation of a 64-bit value?

最佳回答

Keep your 64-bit number as separate high and low partitions. To rotate left N when N < 32:

hi_rot = ((hi << N) | (lo >>> (32-N))) & (0xFFFFFFFF)

lo_rot = ((lo << N) | (hi >>> (32-N))) & (0xFFFFFFFF)

If N >= 32, then subtract 32 from N, swap hi and lo, and then do the above.

问题回答

I believe so, though not the most efficient way, convert the number to a string in binary form (64-bits), use substring to move the char at the beginning and append it to the end (for left rotation) and convert the binary form back to number. I am sure you can figure out how to convert a decimal number to its binary form into a string and back.

As @Doug Currie put it you need to represent the 64-bit number as two numbers, then do bit-wise operations on them. The code I ve used is:

//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..
};

To use it try running: console.log(new Long(0,1).rotateLeft(4)); then inspecting the _high and _low properties.

The only way I think it can be done is to create an int64 class which internally contains two 32 bit integers and performs shifting by carrying between them.

Here s a values based rotate.

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;




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签