How to count the total bits of a number in Node.js

1 Answer

0 votes
function totalBits(num) {      
    return Math.floor(Math.log2(num)) + 1;
}
     
   
let n = 12; // 1100 
console.log(totalBits(n));
  
n = 386; // 1 1000 0010
console.log(totalBits(n));
  
  
   
    
    
/*
run:
    
4
9
    
*/

 



answered Dec 6, 2021 by avibootz
...