Math.clz32()
Die Math.clz32()
Funktion zählt die führenden Nullbits in der 32-Bit binär Repräsentation einer Nummer.
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)
Parameter
x
- Eine Nummer.
Rückgabewert
Die Anzahl der führenden Nullbits in der 32-Bit binör Repräsentation der übergebenen Zahl.
Beschreibung
"clz32
" steht für CountLeadingZeroes32 (AnzahlFührenderNullen32)
.
Wenn x
keine Nummer ist, wird x
in eine Nummer konvertiert. Danach wird diese Nummer in einen 32-Bit vorzeichenlose Ganzzahl (unsigned integer) konvertiert.
Wenn die konvertierte 32-Bit vorzeichenlose Zahl 0
ist, so wird die Funktion 32 zurück geben, weil alle Bits 0
sind.
Diese Funktion ist nützlich für Systeme, die in zu JavaScript kompilieren (z. B. Emscripten).
Beispiele
Einsatz von 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
Polyfill
Der folgende Polyfill ist der effizienteste.
if (!Math.clz32) {
Math.clz32 = 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 - Math.floor(Math.log(x >>> 0) * Math.LOG2E);
};
}
Spezifikationen
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) Die Definition von 'Math.clz32' in dieser Spezifikation. |
Standard | Initiale Definition. |
ECMAScript (ECMA-262) Die Definition von 'Math.clz32' in dieser Spezifikation. |
Lebender Standard |
Browserkompatibilität
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.