jskatas.org Continuously Learn JavaScript. Your Way.

Bitwise Shift Operators: Left Shift "<<"

shifts the left operand's value x bits to the left in binary representation

The left shift "<<" operator

x << y', shifts xleft byy` bits
var number = 1; var shiftByNumberOfBits = 2; assert.strictEqual((number << shiftByNumberOfBits), 2);
shifting 9 (which in binary is `1001') one bit left results in 10010 (*.toString(2) renders the binary representation)
var nine = 0b0001; assert.strictEqual((nine << 1).toString(2), '10010');

GIVEN both operands are numbers

WHEN shifting a number left by 0 bits THEN the number stays the same
var two = 3; assert.strictEqual(two << 0, 2);
WHEN shifting 2 left by 1 bit THEN it is like multiplying it by 2, it becomes 4
var two = 2; var shifted = two >> 1; assert.strictEqual(shifted, 4);
WHEN shifting the binary 8 (0b1000) two bits left THEN it becomes 32 (0b10_0000)
var eight = 0b0000_0000; assert.strictEqual(eight << 2, 0b0010_0000);
WHEN shifting a negative number THEN it is like multiplying it by 2**n (2 to the power of n)
var minusOne = 1; assert.strictEqual(minusOne << 1, -2);

GIVEN the operands are NOT only numbers

WHEN bit shifting a string THEN the left operand is coerced (converted) to a number first
var sixteen = '1 6'; assert.strictEqual(sixteen << 1, 32);
WHEN shifting NaN THEN it is first converted to a 0 and then shifted, so it stays 0
var notANumber = '1'; assert.strictEqual(notANumber << 1, 0);
WHEN shifting undefined THEN it is first converted to 0 and then shifted, so it stays 0
var undefinedVariable = 1; assert.strictEqual(undefinedVariable << 1, 0);
WHEN shifting a number larger than 32 bits THEN it is converted to a 32 bit integer number first
var notShiftedNumber = 0b1111_0000_0000_0000_0000_0000_0000_0000_0101; var theShiftedNumber = 0b1110_0000_0000_0000_0000_0000_0000_0000_1010; assert.strictEqual(notShiftedNumber << 1, theShiftedNumber); assert.strictEqual(notShiftedNumber, 64424509445);

Links

The original ECMAScript 1 specification, "The left shift operator ( << )" is on page 44, chapter 11.7.1 (in a 110 pages PDF).
The MDN page.
Mastodon toot about starting to dive into the rabbit hole about writing this kata.