Bitwise operators work differently with BigInts
MDN URL
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
What specific section or headline is this issue about?
Operators
What information was incorrect, unhelpful, or incomplete?
Bitwise operators are supported as well, except >>> (zero-fill right shift), as every BigInt value is signed.
I would argue that this is incorrect. In the Description section of the documentation for most if not all of the bitwise operators states that
The operands are converted to 32-bit integers and expressed by a series of bits (zeroes and ones). Numbers with more than 32 bits get their most significant bits discarded.
Once the most significant digits have been discarded, the results of any operation are likely to be wrong.
What did you expect to see?
It is good that the article refers to bitwise operations, but it would be helpful if it explicitly stated that the bitwise operators will first convert the value to a 32 bit integer.
Do you have any supporting links, references, or citations?
Bitwise XOR (^) Bitwise OR (|) Bitwise AND (&) Bitwise NOT (~) Left shift (<<)
Do you have anything more you want to share?
No response
MDN metadata
Page report details
- Folder:
en-us/web/javascript/reference/global_objects/bigint - MDN URL: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
- GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/web/javascript/reference/global_objects/bigint/index.md
- Last commit: https://github.com/mdn/content/commit/d19dc31570f62196a5837be38bd0b11c45e67b05
- Document last modified: 2023-02-21T07:09:00.000Z
The description of truncating to 32 bits does not apply to bigints—you would have known with some simple test code. We need to revise all documentation on operators to incorporate bigint changes.
My simple test was:
BigInt(1) << 1
This returns:
Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions at
:1:4
You need to use 1n << 1n.