# Is bitwise shift's left operand's encoding well-defined?

**URL:** <https://es.discourse.group/t/is-bitwise-shifts-left-operands-encoding-well-defined/1373>\
**Category:** Spec Reading\
**Created:** [June 20, 2022, 2:09pm UTC](https://es.discourse.group/t/is-bitwise-shifts-left-operands-encoding-well-defined/1373 "2022-06-20T14:09:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh-Cena](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/josh-cena/32/1408_2.png) [@Josh-Cena](https://es.discourse.group/u/Josh-Cena)\
**Post date:** [June 20, 2022, 2:09pm UTC](https://es.discourse.group/t/is-bitwise-shifts-left-operands-encoding-well-defined/1373/1 "2022-06-20T14:09:19Z")

</div>

I'm reading the specification on bitwise shift operators, and I'm unsure whether integers are ever required to be two's complements.

In [`NumberBitwiseOp`](https://tc39.es/ecma262/#sec-numberbitwiseop), it's explicitly mentioned that

> Let _lbits_ be the 32-bit two's complement bit string representing ℝ(_lnum_).

Which probably means that for `&`, `|`, and `^`, I can confidently say that the operation is working on 32-bit two's complements.

In [`BigInt::leftShift`](https://tc39.es/ecma262/#sec-numeric-types-bigint-leftShift), it's mentioned that

> Semantics here should be equivalent to a bitwise shift, treating the BigInt as an infinite length string of binary two's complement digits.

Which _implies_ that bitwise shift should operate on two's complements.

However, in [`Number::leftShift`](https://tc39.es/ecma262/#sec-numeric-types-bigint-leftShift), it's only mentioned that

> Return the result of left shifting _lnum_ by _shiftCount_ bits.

Where _lnum_ is the result of `ToInt32`, the latter of which is defined by the mathematical value, not by its binary encoding. Unlike `NumberBitwiseOp`, the operation here is working on a number, not a bit string, and the encoding is not specified. The next sentence says " The mathematical value of the result is exactly representable as a 32-bit two's complement bit string", but that sounds like an invariant assertion instead of a normative specification.

Is that an editorial mistake, or have I overlooked something? Does "left shift" necessarily imply "two's complement"?

---

<div class="post-metadata">

**Author:** ![jmdyck](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jmdyck/32/796_2.png) [@jmdyck](https://es.discourse.group/u/jmdyck)\
**Post date:** [June 23, 2022, 2:26am UTC](https://es.discourse.group/t/is-bitwise-shifts-left-operands-encoding-well-defined/1373/2 "2022-06-23T02:26:40Z")

</div>

I think [PR #2274](https://github.com/tc39/ecma262/pull/2274) would resolve this problem.

---

<div class="post-metadata">

**Author:** ![Josh-Cena](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/josh-cena/32/1408_2.png) [@Josh-Cena](https://es.discourse.group/u/Josh-Cena)\
**Post date:** [June 23, 2022, 3:45am UTC](https://es.discourse.group/t/is-bitwise-shifts-left-operands-encoding-well-defined/1373/3 "2022-06-23T03:45:47Z")

</div>

Ah, great that I'm not going insane. Thanks for the link.
