Skip to content

Instantly share code, notes, and snippets.

@w8r
Created August 17, 2018 08:26
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 w8r/2d73d77a87779fb77bc0ca0814797371 to your computer and use it in GitHub Desktop.
Save w8r/2d73d77a87779fb77bc0ca0814797371 to your computer and use it in GitHub Desktop.
Count leading zeros
/**
* Count leading zeros in binary representation
* @param {number} m
* @return {number} 0-32
*/
export default function clz(m) {
let c = 1 << 31, i;
for (let i = 0; i < 32; i += 1) {
if (c & m) return i;
c >>>= 1;
}
return 32;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment