Skip to content

Instantly share code, notes, and snippets.

@alexgmcm
Created September 18, 2018 13:48
Show Gist options
  • Save alexgmcm/8f204e39a6b91322c954eaf7d3403467 to your computer and use it in GitHub Desktop.
Save alexgmcm/8f204e39a6b91322c954eaf7d3403467 to your computer and use it in GitHub Desktop.
LeftRotate.js
function LeftRotate(i, r, bitSize) {
if (i.isNegative()){
//take twos complement representation
i=bigInt(1).shiftLeft(32).subtract(i.abs()).and(bigInt("00000000ffffffff",16));
var mask=bigInt(1).shiftLeft(bitSize).subtract(1);
var x=i.shiftLeft(r).or(i.shiftRight(bitSize-r)).and(mask);
var x_arr=x.toArray(2)
var neg=false
if (x_arr['value'][0]==1){
//convert back
//console.log(x_arr)
for(var i=0; i < x_arr['value'].length; i++) {
x_arr['value'][i]= x_arr['value'][i]==1 ? 0 : 1
}
return bigInt.fromArray(x_arr['value'],2,false).add(1).multiply(-1);
}
}
else{
var mask=bigInt(1).shiftLeft(bitSize).subtract(1);
return i.shiftLeft(r).or(i.shiftRight(bitSize-r)).and(mask);
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment