BigInt.asUintN()
åºçº¿
广æ³å¯ç¨
èª 2020å¹´9æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
BigInt.asUintN éææ¹æ³å° BigInt 转æ¢ä¸ºä¸ä¸ª 0 å 2^width-1 ä¹é´çæ ç¬¦å·æ´æ°ã
å°è¯ä¸ä¸
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));
// 1n
console.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(width, bigint);
åæ°
è¿åå¼
bigint 模 (modulo) 2^width ä½ä¸ºæ ç¬¦å·æ´æ°çå¼ã
示ä¾
>ä¿æå¨ 64 ä½èå´å
BigInt.asUintN() æ¹æ³å¯¹äºä¿æå¨ 64 ä½ (64-bit) ç®æ°èå´å
é常æç¨ã
js
const max = 2n ** 64n - 1n;
BigInt.asUintN(64, max);
// ⪠18446744073709551615n
BigInt.asUintN(64, max + 1n);
// ⪠0n
// zero because of overflow
è§è
| è§è |
|---|
| ECMAScript® 2027 Language Specification> # sec-bigint.asuintn> |