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 BigInt.asUintN()
statische Methode kürzt einen BigInt
Wert auf die angegebene Anzahl der niederwertigsten Bits 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 BigInt.asUintN
Methode kürzt einen BigInt
Wert auf die angegebene Anzahl von Bits 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
Note:
BigInt
Werte sind in binärer Zwei-Komplement-Darstellung immer codiert.
Im Gegensatz zu ähnlichen Sprach-APIs wie Number.prototype.toExponential()
ist asUintN
eine statische Eigenschaft von BigInt
, sodass Sie es immer als BigInt.asUintN()
verwenden und nicht als Methode eines BigInt-Werts. Das Bereitstellen von asUintN()
als "Standardbibliotheksfunktion" ermöglicht Interop mit asm.js.
Beispiele
Im Bereich von 64-Bit bleiben
Die BigInt.asUintN()
Methode 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 |