All bitwise shift operators
a bitwise shift operator works on two operands shifting their bits
GIVEN we shift a number left
WHEN the number is shifted 1 bit to the left THEN this is like a multiplication by 2
WHEN the number is shifted THEN the bits just "wander" left
WHEN we shift a number by 0 bits THEN the number becomes a 32-bit number
GIVEN we shift a number right
WHEN the number is shifted 1 bit to the right THEN this is like a division by 2
WHEN shifting a number right THEN the 1s fall off on the right
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)
WHEN shifting a negative number THEN also the sign bit (the left most bit) is shifted AND the number becomes unsigned
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
- Left Shift "<<"
- Right Shift ">>"
- All bitwise shift operators
Rest operator
Spread operator
Difficulty Level
BEGINNER
First Published
4 September 2023
Stats
8 tests to solve