The right shift ">>" operator
x >> y
, shifts x
right by y
bits
shifting 15 (in binary: 1111) right by 4 makes it 0, since all the 1s just "fall off"
shifting a negative number it stays negative
GIVEN both operands are numbers
WHEN shifting right by 1 THEN this is like a division by 2
WHEN shifting right an odd number by 1 bit THEN this is like a division by 2 without rounding
GIVEN the operands are NOT only numbers
THEN the operands are converted to numbers
WHEN an operand is an array THEN this is also converted to a number
WHEN the right operand can NOT be converted to a number THEN it becomes 0
Links
The original ECMAScript 1 specification, "The right shift operator ( << )" is on page 44, chapter 11.7.2 (in a 110 pages PDF).
The MDN page.
Mastodon toot about starting to dive into the rabbit hole about writing this kata.
Required Knowledge
- to number (Type conversion)
Related Katas
Unary Operators
Bitwise Shift Operators
- Left Shift "<<"
- Right Shift ">>"
- All bitwise shift operators
Rest operator
Spread operator
Difficulty Level
INTERMEDIATE
First Published
4 September 2023
Stats
8 tests to solve