Skip to content

Instantly share code, notes, and snippets.

@mafintosh
Last active February 25, 2019 19:53
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 mafintosh/6566c34cca66b7c561f68cdd106b0822 to your computer and use it in GitHub Desktop.
Save mafintosh/6566c34cca66b7c561f68cdd106b0822 to your computer and use it in GitHub Desktop.
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