Skip to content

Instantly share code, notes, and snippets.

@CatTail
Created December 1, 2012 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CatTail/4179750 to your computer and use it in GitHub Desktop.
Save CatTail/4179750 to your computer and use it in GitHub Desktop.
Javascript: bitshift
var dec1 = 5234542352345;
var bin1 = dec1.toString(2);
// chop into 32 bit
var bin2 = bin1.slice(bin1.length-32);
var dec2 = parseInt(bin2, 2);
var dec3 = dec2 >> 0;
var bin3 = dec3.toString(2);
console.log(dec1);
console.log(dec2);
console.log(dec3);
console.log(bin1);
console.log(bin2);
console.log(bin3);
// Output:
// 5234542352345
// 3272185817
// -1022781479
// 1001100001011000011000010011001011111011001
// 11000011000010011001011111011001
// -111100111101100110100000100111
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment