aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/bigint/asintn/index.html
blob: cab753a464b646748e0b2448e5ef98b60da6efa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
---
title: BigInt.asIntN()
slug: Web/JavaScript/Reference/Global_Objects/BigInt/asIntN
translation_of: Web/JavaScript/Reference/Global_Objects/BigInt/asIntN
---
<div>{{JSRef}}</div>

<p><strong><code>BigInt.asIntN</code></strong> 静态方法将 <code>BigInt</code> 值转换为一个 -<code>2<sup>width-1</sup></code> 与 <code>2<sup>width-1</sup>-1</code> 之间的有符号整数。</p>

<div>{{EmbedInteractiveExample("pages/js/bigint-asintn.html")}}</div>



<h2 id="语法">语法</h2>

<pre class="syntaxbox">BigInt.asIntN(<var>width</var>, <var>bigint</var>);</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>width</code></dt>
 <dd>可存储整数的位数。</dd>
 <dt><code>bigint</code></dt>
 <dd>要存储在指定位数上的整数。</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p><code>bigint</code> 模(modulo) 2<sup><code>width</code></sup> 作为有符号整数的值。</p>

<h2 id="例子">例子</h2>

<h3 id="保持在64位范围内">保持在64位范围内</h3>

<p><code>BigInt.asIntN()</code> 方法对于保持在64位(64-bit)算数范围内非常有用。</p>

<pre class="brush: js">const max = 2n ** (64n - 1n) - 1n;

BigInt.asIntN(64, max);
// ↪ 9223372036854775807n

BigInt.asIntN(64, max + 1n);
// ↪ -9223372036854775808n
// negative because of overflow
</pre>

<h2 id="标准">标准</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-bigint.asintn', 'BigInt.asIntN()')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器支持">浏览器支持</h2>



<p>{{Compat("javascript.builtins.BigInt.asIntN")}}</p>

<h2 id="请参阅">请参阅</h2>

<ul>
 <li>{{JSxRef("BigInt")}}</li>
 <li>{{JSxRef("BigInt.asUintN()")}}</li>
</ul>