Created
August 17, 2018 08:26
-
-
Save w8r/2d73d77a87779fb77bc0ca0814797371 to your computer and use it in GitHub Desktop.
Count leading zeros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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