Last active
February 25, 2019 19:53
-
-
Save mafintosh/6566c34cca66b7c561f68cdd106b0822 to your computer and use it in GitHub Desktop.
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
console.log(Math.clz32(42424), clz(42424)) | |
function clz(x) { | |
// Let n be ToUint32(x). | |
// Let p be the number of leading zero bits in | |
// the 32-bit binary representation of n. | |
// Return p. | |
if (x == null || x === 0) { | |
return 32; | |
} | |
// should be | |
// return 31 - (Math.log(x >>> 0) / Math.LN2 | 0); // the "| 0" acts like math.floor | |
return 31 - Math.log(x >>> 0) / Math.LN2 | 0; // the "| 0" acts like math.floor | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment