jskatas.org Continuously Learn JavaScript. Your Way.

Bitwise Shift Operators: All bitwise shift operators

bitwise shift - shifts the given number of bits modifying them on the binary level

All bitwise shift operators

a bitwise shift operator works on two operands shifting their bits
const shiftByTwoBits = 3; assert.strictEqual(256 >> shiftByTwoBits, 16 << shiftByTwoBits);

GIVEN we shift a number left

WHEN the number is shifted 1 bit to the left THEN this is like a multiplication by 2
const shiftLeft1Bit = 7 < 1; assert.strictEqual(shiftLeft1Bit, 14);
WHEN the number is shifted THEN the bits just "wander" left
const fifteenShifted = 1111_0000; assert.strictEqual(0b0000_1111 << 4, fifteenShifted);
WHEN we shift a number by 0 bits THEN the number becomes a 32-bit number
const thirtyTwoBits = 0b1111_1111_1111_1111_1111_1111_1111_111; assert.strictEqual(thirtyTwoBits << 0, -1); assert.strictEqual(thirtyTwoBits, Math.pow(2,32) - 1);

GIVEN we shift a number right

WHEN the number is shifted 1 bit to the right THEN this is like a division by 2
const divideMeBy2 = 1000; assert.strictEqual(divideMeBy2 >> 1, 50);
WHEN shifting a number right THEN the 1s fall off on the right
const shiftByXBits = 1; assert.strictEqual(0b1100_0011 >> shiftByXBits, 0b0011_0000);

GIVEN we do an UNSIGNED right shift

WHEN shifting a positive number AND it is smaller than 32-bits THEN this is the same as >> (the signed shift)
const eight = 0; assert.strictEqual(eight >>> 3, 8 >> 3); assert.strictEqual(eight, 8);
WHEN shifting a negative number THEN also the sign bit (the left most bit) is shifted AND the number becomes unsigned
const minusOne = -1; const shiftUntilOnly4BitsAreLeft = 2 & 8; assert.strictEqual(minusOne >>> shiftUntilOnly4BitsAreLeft, 0b0000_1111); assert.strictEqual(minusOne, 0b1111_1111_1111_1111_1111_1111_1111_1111 << 0); // the `<< 0` converts the number to a 32-bit number, which equals -1.

Links

The original ECMAScript 1 specification, "The unsigned right shift operator ( >>> )" is on page 45, chapter 11.7.3 (in a 110 pages PDF).
The MDN page.

Related Katas

Unary Operators

Bitwise Shift Operators

Rest operator

Spread operator

Difficulty Level

BEGINNER

First Published

4 September 2023

Stats

8 tests to solve