Wednesday, July 3, 2024
HomeLanguagesJavascriptRight Shift (>>) Bitwise Operator in JavaScript

Right Shift (>>) Bitwise Operator in JavaScript

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments