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()
schneidet einen BigInt
-Wert auf die angegebene Anzahl an niederwertigen Bits ab und gibt diesen Wert als vorzeichenlose Ganzzahl 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 vorzeichenlose Ganzzahl.
Ausnahmen
RangeError
-
Wird ausgelöst, wenn
bits
negativ oder größer als 253 - 1 ist.
Beschreibung
Die Methode BigInt.asUintN
schneidet einen BigInt
-Wert auf die angegebene Anzahl von Bits ab und interpretiert das Ergebnis als vorzeichenlose Ganzzahl. Vorzeichenlose Ganzzahlen 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
Hinweis:
BigInt
-Werte werden stets als Zweierkomplement im Binärformat kodiert.
Im Gegensatz zu ähnlichen API-Funktionen in anderen Sprachen wie Number.prototype.toExponential()
ist asUintN
eine statische Eigenschaft von BigInt
, sodass Sie sie immer als BigInt.asUintN()
und nicht als Methode eines BigInt-Werts verwenden. Das Bereitstellen von asUintN()
als "Standard-Bibliotheksfunktion" ermöglicht die Interoperabilität mit asm.js.
Beispiele
Im Bereich von 64-Bit bleiben
Die Methode BigInt.asUintN()
kann nützlich sein, um im Bereich der 64-Bit-Arithmetik 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® 2026 Language Specification # sec-bigint.asuintn |