BigInt.asUintN()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.
Die statische Methode BigInt.asUintN()
kürzt einen BigInt
-Wert auf die angegebene Anzahl der niederwertigsten Bits und gibt diesen Wert als unsigned Integer zurück.
Probieren Sie es aus
const U64_CEIL = 2n ** 64n;
console.log(BigInt.asUintN(64, U64_CEIL - 1n));
// 18446744073709551615n (2n ** 64n - 1n, the maximum non-wrapping value)
console.log(BigInt.asUintN(64, U64_CEIL));
// 0n (wraps to zero)
console.log(BigInt.asUintN(64, U64_CEIL + 1n));
// 1n
console.log(BigInt.asUintN(64, U64_CEIL * 2n));
// 0n (wraps on multiples)
console.log(BigInt.asUintN(64, U64_CEIL * -42n));
// 0n (also wraps on negative multiples)
Syntax
BigInt.asUintN(bits, bigint)
Parameter
Rückgabewert
Der Wert von bigint
modulo 2^bits
, als unsigned Integer.
Ausnahmen
RangeError
-
Wird ausgelöst, wenn
bits
negativ oder größer als 253 - 1 ist.
Beschreibung
Die Methode BigInt.asUintN
kürzt einen BigInt
-Wert auf die angegebene Anzahl an Bits und interpretiert das Ergebnis als unsigned Integer. Unsigned Integer haben keine Vorzeichenbits und sind immer nicht-negativ. Zum Beispiel wird bei BigInt.asUintN(4, 25n)
der Wert 25n
auf 9n
gekürzt:
25n = 00011001 (base 2) ^==== Use only the four remaining bits ===> 1001 (base 2) = 9n
Note:
BigInt
-Werte werden im Binärformat immer als Zweierkomplement kodiert.
Im Gegensatz zu ähnlichen APIs in anderen Sprachen, wie Number.prototype.toExponential()
, ist asUintN
eine statische Eigenschaft von BigInt
. Deshalb wird sie immer als BigInt.asUintN()
und nicht als Methode eines BigInt
-Werts verwendet. Die Bereitstellung von asUintN()
als „Standardbibliotheksfunktion“ ermöglicht Interop mit asm.js.
Beispiele
Innerhalb eines 64-Bit-Bereichs bleiben
Die Methode BigInt.asUintN()
kann nützlich sein, um innerhalb des Bereichs arithmetischer 64-Bit-Operationen zu bleiben.
const max = 2n ** 64n - 1n;
BigInt.asUintN(64, max); // 18446744073709551615n
BigInt.asUintN(64, max + 1n); // 0n
// zero because of overflow: the lowest 64 bits are all zeros
Spezifikationen
Specification |
---|
ECMAScript® 2025 Language Specification # sec-bigint.asuintn |
Browser-Kompatibilität
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
asUintN |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support