Let's say, we receive a binary representation of a number - how we can get the highest bit set in it (e.g. the biggest power of 2)?
For example: integer 9
-> binary 00001001
-> most left bit position 3
CodePudding user response:
Use the less-known native function Math.clz32
:
const msb = n => 31 - Math.clz32(n);
console.log(msb(0b1001)); // 3