Javascript BigInt asIntN()

Introduction

The Javascript BigInt.asIntN() static method converts a BigInt value to a signed integer.

BigInt.asIntN(width, bigint);
Parameter Meaning
width The amount of bits available for the integer size.
bigint The big integer

Returns

The value of bigint modulo 2width as a signed integer.

Staying in 64-bit ranges

The BigInt.asIntN() method can be useful to stay in the range of 64-bit arithmetic.

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

let a = BigInt.asIntN(64, max);
console.log(a);/*  w w  w. j a  va 2 s . c  om*/


a = BigInt.asIntN(64, max + 1n);
console.log(a);



PreviousNext

Related