このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。
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 2020年9月.
BigInt.asUintN() は静的メソッドで、長整数を符号なし整数に丸め、その値を返します。
In this article
試してみましょう
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));// 1nconsole.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)構文
BigInt.asUintN(bits, bigint)引数
返値
bigint の値を2 ** bits で割った剰余の値の符号なし整数です。
例外
RangeErrorbitsが負または 253 - 1 より大きい場合に発生します。
解説
BigInt.asUintN メソッドは、長整数を指定されたビット数で切り捨て、結果を符号なし整数として解釈します。符号なし整数は符号ビットを持たず、常に非負です。例えば、BigInt.asUintN(4, 25n) の場合、値25n は9n に切り捨てられます。
25n = 00011001 (base 2) ^==== 最後の 4 ビットのみを使用===> 1001 (base 2) = 9n
メモ:BigInt の値は常に 2 進数の 2 の補数形式でエンコードされます。
類似の言語 API(例:Number.prototype.toExponential())とは異なり、asUintN は BigInt の静的プロパティであるため、BigInt 値のメソッドとしてではなく、常にBigInt.asUintN() として使用します。asUintN() を「標準ライブラリ関数」として公開することで、asm.js との相互運用性が実現されます。
例
>64 ビットの範囲に収める
BigInt.asUintN() メソッドは、64 ビットの数値の範囲に収めるのに便利です。
const max = 2n ** 64n - 1n;BigInt.asUintN(64, max);// ↪ 18446744073709551615nBigInt.asUintN(64, max + 1n);// ↪ 0n// オーバーフローするのでゼロになる仕様書
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-bigint.asuintn> |