blob: 3465ce6c21ffd2766f0f98e1a7abf22d2cbbb650 (
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
|
---
title: BigInt.asIntN()
slug: Web/JavaScript/Reference/Global_Objects/BigInt/asIntN
tags:
- BigInt
- JavaScript
- Méthode
- Reference
translation_of: Web/JavaScript/Reference/Global_Objects/BigInt/asIntN
original_slug: Web/JavaScript/Reference/Objets_globaux/BigInt/asIntN
---
{{JSRef}}
La méthode statique **`BigInt.asIntN()`** permet d'écréter un nombre `BigInt` pour obtenir un entier signé entre 2^(largeur-1) et 2^(largeur-1)-1.
{{EmbedInteractiveExample("pages/js/bigint-asintn.html")}}
## Syntaxe
var resultat = BigInt.asIntN(largeur, bigint);
### Paramètres
- `largeur`
- : La quantité de bits disponible pour stocker l'entier.
- `bigint`
- : L'entier qu'on souhaite stocker sur le nombre de bits indiqués.
### Valeur de retour
La valeur de `bigint` modulo 2^`largeur` comme entier signé.
## Exemples
La méthode `BigInt.asIntN()` peut être utile pour rester dans une arithmétique sur 64 bits :
```js
const max = 2n ** (64n - 1n) - 1n;
BigInt.asIntN(64, max);
// ↪ 9223372036854775807n
BigInt.asIntN(64, max + 1n);
// ↪ -9223372036854775807n
// négatif car dépassement sur le nombre de bits
```
## Spécifications
| Spécification | État |
| ---------------------------------------------------------------------------- | ------------------------ |
| [BigInt proposal](https://tc39.github.io/proposal-bigint/#sec-bigint.asintn) | Proposition de niveau 3. |
## Compatibilité des navigateurs
{{Compat("javascript.builtins.BigInt.asIntN")}}
## Voir aussi
- {{JSxRef("BigInt")}}
- {{JSxRef("BigInt.asUintN()")}}
|