The bitwise Left Shift (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
<<) operator shifts the binary representation of its first operand to the left by the number of bits specified by its second operand. Zero bits are shifted in from the right. The internal execution mechanics and limits of this operation depend strictly on whether the operands are of type Number or BigInt.
Execution Mechanics: Number Operands
When evaluating standard JavaScriptNumber types, the engine performs the following sequence of internal conversions:
- Operand Truncation:
operand1is converted to a 32-bit signed integer using Two’s complement representation. Any fractional components or values exceeding the 32-bit boundary are truncated. - Shift Masking:
operand2is converted to an unsigned 32-bit integer. JavaScript then applies a bitwise AND mask of0x1F(31) to this value. This ensures the shift amount is strictly between0and31bits. For example, shifting by33is evaluated as shifting by1. - Bit Manipulation: The 32-bit representation of
operand1is shifted left by the masked value ofoperand2. Excess bits shifted off to the left are discarded. - Return Type: The result is evaluated and returned as a standard JavaScript
Numberrepresenting the new 32-bit signed integer.
Execution Mechanics: BigInt Operands
When evaluatingBigInt types, the operator leverages arbitrary precision. Both operands must be BigInt values; mixing Number and BigInt throws a TypeError.
- No Truncation:
operand1is not bound by the 32-bit limit. It retains its full precision, and no bits are discarded from the left during the shift. - No Shift Masking:
operand2is not masked by modulo 32. It dictates the exact number of bits to shift, allowing for bit manipulation well beyond the standard 32-bit limit. Ifoperand2is negative, the operation evaluates as a right shift (>>), effectively shifting the bits to the right by the absolute value ofoperand2. - Return Type: The result is evaluated and returned as a
BigInt.
Mathematical Equivalence
Mathematically,a << b is equivalent to .
- For
Numberoperands, this equivalence only holds if all of the following conditions are met:ais an integer within the 32-bit signed integer range. Any fractional components are truncated before the shift (e.g.,1.5 << 1evaluates to2, whereas ). Values exceeding the 32-bit boundary are truncated to their lower 32 bits before the shift operation begins.bis an integer between0and31inclusive. Fractional values are truncated during internal conversion (e.g.,1 << 1.5evaluates to2, not ). Negative values or values are masked by modulo 32 (e.g.,1 << -1masks to a shift of 31, yielding-2147483648, not ).- The result does not overflow. The resulting value must fit within the 32-bit signed integer limit ( to ). If an overflow occurs, the sign bit may flip, resulting in a negative number.
- For
BigIntoperands, this equivalence holds universally for any integerb. The type dynamically scales to accommodate larger values without overflowing. Ifbis negative, the operation mirrors mathematical division by powers of two (with flooring applied), effectively acting as a right shift.
Syntax Visualization
Master JavaScript with Deep Grasping Methodology!Learn More





