Open In App

Right Shift (>>) Bitwise Operator in JavaScript

Last Updated : 23 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript bitwise right shift operator is used to operate on two operands where the left operand is the number and the right operand specifies the number of bits to shift towards the right. A copy of old leftmost bits is maintained and they have added again the shifting is performed. The sign bit is not changed so it is also called sign propagating or sign extending right shift. If we right-shift a positive number by 1 it will be divided by 2

Let’s look at the table below to better understand the output of the Sign Propagating Right Shift Operation.

A 6 ( 00000000000000000000000000000110 )
B 1 ( 00000000000000000000000000000001 )
OUTPUT ( A >> B ) 3 ( 00000000000000000000000000000011 )

Syntax:

a>>b

Example 1:In this example, we will use the sign propagating right shift operator on the numbers.

Javascript




let a = 4;
let b = -32
console.log(a>>1);
console.log(b>>4);


Output:

2
-2

Example 2: In this example, we will create a function to divide the number by 2.

Javascript




function divByTwo(n) {
    return n>>1;
}
console.log(divByTwo(5));
console.log(divByTwo(-45));
console.log(divByTwo(88));
console.log(divByTwo(-23));


Output: The value returned after right shift is the floor value which will be returned after dividing by two.

2
-23
44
-12

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

We have a complete list of JavaScript Bitwise Operators, to check those please go through, the JavaScript Bitwise Operators article


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads