This page was translated from English by the community.Learn more and join the MDN Web Docs community.
BigInt.asIntN()
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.asIntN() 정적 메서드는BigInt 값을 주어진 최하위 비트 수로 자르고 해당 값을 부호 있는 정수로 반환합니다.
In this article
시도해 보기
const I64_CEIL = 2n ** 63n;console.log(BigInt.asIntN(64, I64_CEIL - 1n));// 9223372036854775807n (2n ** 64n - 1n, the maximum non-wrapping value)console.log(BigInt.asIntN(64, I64_CEIL));// -9223372036854775808n (wraps to min value)console.log(BigInt.asIntN(64, I64_CEIL + 1n));// -9223372036854775807n (min value + 1n)console.log(BigInt.asIntN(64, I64_CEIL * 2n));// 0n (wrapped around to zero)console.log(BigInt.asIntN(64, -I64_CEIL * -42n));// 0n (also wraps on negative multiples)구문
BigInt.asIntN(bits, bigint)매개변수
반환 값
부호 있는 정수인,bigint와 2^bits의 나머지 연산 값입니다.
예외
RangeErrorbits가 음수이거나 253 - 1 보다 크면 발생합니다.
설명
BigInt.asIntN 메서드는BigInt 값을 주어진 비트 수로 잘라내고 그 결과를 부호 있는 정수로 해석합니다. 예를 들어,BigInt.asIntN(3, 25n)의 경우25n 값은1n으로 잘립니다.
25n = 00011001 (이진수) ^=== 오직 남은 3개의 비트만 사용합니다===> 001 (이진수) = 1n
남은 숫자의 첫 번째 비트가1이라면, 결과는 음수입니다. 예를 들어BigInt.asIntN(4, 25n)는1001이 2의 보수 체계에서는-7로 인코딩되기 때문에-7n이 산출됩니다.
25n = 00011001 (이진수) ^==== 오직 남은 4개의 비트만 사용합니다===> 1001 (이진수) = -7n
참고 :BigInt 값은 언제나 이진수 2의 보수로 인코딩됩니다.
Number.prototype.toExponential()와 같은 유사한 언어 API와 달리asIntN은BigInt의 정적 속성이므로 항상 BigInt 값의 메서드가 아닌BigInt.asIntN()으로 사용합니다.asIntN()을 "표준 라이브러리 함수"로 노출하면asm.js와의 상호 운용을 할 수 있습니다.
예제
>64비트 범위에 머무르기
64비트 연산 범위를 유지하는 데BigInt.asIntN() 메서드가 유용할 수 있습니다.
const max = 2n ** (64n - 1n) - 1n;BigInt.asIntN(64, max); // 9223372036854775807nBigInt.asIntN(64, max + 1n); // -9223372036854775808n// 2^63의 64번째 비트가 1이기 때문에 음수입니다.명세서
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-bigint.asintn> |