This is an experimental technology, part of the Harmony (ECMAScript 6) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.
Summary
The Math.clz32()
function returns the number of leading zero bits in the 32-bit binary representation of a number.
Syntax
Math.clz32 (x)
Parameters
- x
- A number.
Description
"clz32" is short for CountLeadingZeroes32.
If x
is not a number, then it will be converted to a number first, then converted to a 32-bit unsigned integer.
If the converted 32-bit unsigned integer is 0
, then return 32
, because all bits are 0
.
This function is particularly useful for systems that compile to JS, like Emscripten.
Examples
Math.clz32(1) // 31 Math.clz32(1000) // 22 Math.clz32() // 32 [NaN, Infinity, -Infinity, 0, -0, null, undefined, "foo", {}, []].filter( function (n) { return Math.clz32(n) !== 32 }) // [] Math.clz32(true) // 31 Math.clz32(3.5) // 30
Polyfill
Math.clz32 = Math.clz32 || function(value) { var value = Number(value) >>> 0; return value ? 32 - value.toString(2).length : 32; }
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript Language Specification 6th Edition (ECMA-262) | Draft | Initial definition. |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 35 | 31 (31) | Not supported | Not supported | Not supported |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | Not supported | Not supported | 31.0 (31) | Not supported | Not supported | Not supported |