BigInt.asIntN()

La méthode statique BigInt.asIntN() permet d'écréter un nombre BigInt pour obtenir un entier signé entre 2^(largeur-1) et 2^(largeur-1)-1.

Exemple interactif

Syntaxe

js

var resultat = BigInt.asIntN(largeur, bigint);

Paramètres

largeur

La quantité de bits disponible pour stocker l'entier.

bigint

L'entier qu'on souhaite stocker sur le nombre de bits indiqués.

Valeur de retour

La valeur de bigint modulo 2^largeur comme entier signé.

Exemples

La méthode BigInt.asIntN() peut être utile pour rester dans une arithmétique sur 64 bits :

js

const max = 2n ** (64n - 1n) - 1n;

BigInt.asIntN(64, max);
// ↪ 9223372036854775807n

BigInt.asIntN(64, max + 1n);
// ↪ -9223372036854775807n
// négatif car dépassement sur le nombre de bits

Spécifications

Specification
ECMAScript Language Specification
# sec-bigint.asintn

Compatibilité des navigateurs

BCD tables only load in the browser

Voir aussi