BigInt.asUintN()
BigInt.asUintN
静的メソッドは、 BigInt 値を 0 から 2width-1 までの間の符号なし整数に丸めるために使われます。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
構文
BigInt.asUintN(width, bigint);
引数
width
- 整数の大きさのために利用できるビットの数。
bigint
- 指定されたビットに収めるよう丸める整数値。
返値
bigint
を 2width
で割った剰余の値の符号なし整数です。
例
64ビットの範囲に収める
BigInt.asUintN()
メソッドは、64ビットの数値の範囲に収めるのに便利です。
const max = 2n ** 64n - 1n;
BigInt.asUintN(64, max);
// ↪ 18446744073709551615n
BigInt.asUintN(64, max + 1n);
// ↪ 0n
// オーバーフローするのでゼロになる
仕様書
ブラウザーの互換性
BCD tables only load in the browser