Nasi wolontariusze nie przetłumaczyli jeszcze tego artykułu na język Polski. Dołącz do nas i pomóż go przetłumaczyć!
Można także przeczytać artykuł w języku: English (US).
The Math.clz32()
function returns the number of leading zero bits in the 32-bit binary representation of a number.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
Math.clz32(x)
Parameters
- x
- A number.
Return value
The number of leading zero bits in the 32-bit binary representation of the given 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
Using Math.clz32()
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
Count Leading Ones And Beyond
At present, there is no Math.clon
for "Count Leading Ones" (named "clon", not "clo", because "clo" and "clz" are too similar especially for non-english-speaking people). However, a CLON function can easily be created by inversing the bits of a number and passing the result to `Math.clz` which will work because the inverse of 1 is 0 and vice-versa. Thus, inversing the bits will inverse the measured quanity 0 (of Math.clz), thereby making Math.clz count the number of ones instead of the number of zeros.
var clz = Math.clz32; function clon(integer){ return clz(~integer); }
Further, this teqnique could be extended to creating jumpless "Count Trailing Zeros" and "Count Trailing Ones" functions as seen below. The ctrz
function below fills in all the high bits with the lowest filled bit, then negates the bits to earase all higher set bits so that clz can then be used.
var clz = Math.clz32; function ctrz(integer){ // count trailing zeros // 1. fill in all the higher bits after the first one integer |= integer << 16; integer |= integer << 8; integer |= integer << 4; integer |= integer << 2; integer |= integer << 1; // 2. Now, inversing the bits reveals the lowest bits return 32 - clz(~integer); } function ctron(integer){ // count trailing ones // No shift-filling-in-with-ones operator is available in // javascript, so the below code is the fastest return ctrz(~integer); /* Alternate implementation for demonstrational purposes: // 1. erase all the higher bits after the first zero integer &= (integer << 16) | 0xffff; integer &= (integer << 8 ) | 0x00ff; integer &= (integer << 4 ) | 0x000f; integer &= (integer << 2 ) | 0x0003; integer &= (integer << 1 ) | 0x0001; // 2. Now, inversing the bits reveals the lowest zeros return 32 - clon(~integer); */ }
Make these helper functions into ASM.JS module; then, you have a true performance masterpiece. Situations like these are exactly what ASM.JS was designed for.
var countTrailsMethods = (function(stdlib, foreign, heap) { "use asm"; var clz = stdlib.Math.clz32; function ctrz(integer) { // count trailing zeros integer = integer | 0; // coerce to an integer // 1. fill in all the higher bits after the first one // ASMjs for some reason does not allow ^=,&=, or |= integer = integer | (integer << 16); integer = integer | (integer << 8); integer = integer | (integer << 4); integer = integer | (integer << 2); integer = integer | (integer << 1); // 2. Now, inversing the bits reveals the lowest bits return 32 - clz(~integer) |0; } function ctron(integer) { // count trailing ones integer = integer | 0; // coerce to an integer return ctrz(~integer) |0; } // unfourtunately, ASM.JS demands slow crummy objects: return {a: ctrz, b: ctron}; })(window, null, null); var ctrz = countTrailsMethods.a; var ctron = countTrailsMethods.b;
Polyfill
The following polyfill is the most efficient.
if (!Math.clz32) Math.clz32 = (function(log, LN2){ return function(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; } return 31 - log(x >>> 0) / LN2 | 0; // the "| 0" acts like math.floor }; })(Math.log, Math.LN2);
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Math.clz32' in that specification. |
Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) The definition of 'Math.clz32' in that specification. |
Draft |
Browser compatibility
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Basic support | Chrome Full support 38 | Edge Full support Yes | Firefox Full support 31 | IE No support No | Opera Full support 25 | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 31 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
Legend
- Full support
- Full support
- No support
- No support