Javascript Bitwise Operator Signed Right Shift

Introduction

The signed right shift is represented by two greater-than signs (>>) and shifts all bits in a 32-bit number to the right while preserving the sign (positive or negative).

A signed right shift is the exact opposite of a left shift.

For example, if 64 is shifted to the right five bits, it becomes 2:

let oldValue = 64;             // equal to binary 1000000 
let newValue = oldValue >> 5;  // equal to binary 10 which is decimal 2 

When bits are shifted, the shift creates empty bits.

The empty bits occur at the left of the number but after the sign bit.

Javascript fills these empty bits with the value in the sign bit to create a complete number.


// Here is a 32 bit representation of numbers
// 28 = 00000000000000000000000000011100
//  7 = 00000000000000000000000000000111

let a = 28 >> 2;

console.log(a); // 7



PreviousNext

Related