How to rotate a number to the right by N bits using bit operation in TypeScript

1 Answer

0 votes
function rotate_right(num: number, n: number) {
    return (num >> n) | (num << (32 - n));
}
 
let num:number = 32;
 
console.log(("00000000" + num.toString(2)).substr(-8));
         
num = rotate_right(num, 2);
 
console.log(("00000000" + num.toString(2)).substr(-8));
 
console.log(num);
 
 
 
 
 
/*
run:
 
"00100000" 
"00001000" 
8 
 
*/

 



answered Jan 12, 2023 by avibootz
edited Jan 12, 2023 by avibootz
...