In C/C++ there is only one right shift operator ‘>>’ which should be used only for positive integers or unsigned integers. Use of right shift operator for negative numbers is not recommended in C/C++, and when used for negative numbers, output is compiler dependent (See this). Unlike C++, Java supports following two right shift operators.

**1) >> (Signed right shift)** In Java, the operator ‘>>’ is signed right shift operator. All integers are signed in Java, and it is fine to use >> for negative numbers. The operator ‘>>’ uses the sign bit (left most bit) to fill the trailing positions after shift. If the number is negative, then 1 is used as a filler and if the number is positive, then 0 is used as a filler. For example, if binary representation of number is **1**0….100, then right shifting it by 2 using >> will make it **11**…….1.

See following Java programs as example ‘>>’

`class` `Test { ` ` ` `public` `static` `void` `main(String args[]) { ` ` ` `int` `x = -` `4` `; ` ` ` `System.out.println(x>>` `1` `); ` ` ` `int` `y = ` `4` `; ` ` ` `System.out.println(y>>` `1` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

Output:

-2 2

**2) >>> (Unsigned right shift)** In Java, the operator ‘>>>’ is unsigned right shift operator. It always fills 0 irrespective of the sign of the number.

`class` `Test { ` ` ` `public` `static` `void` `main(String args[]) { ` ` ` ` ` `// x is stored using 32 bit 2's complement form. ` ` ` `// Binary representation of -1 is all 1s (111..1) ` ` ` `int` `x = -` `1` `; ` ` ` ` ` `System.out.println(x>>>` `29` `); ` `// The value of 'x>>>29' is 00...0111 ` ` ` `System.out.println(x>>>` `30` `); ` `// The value of 'x>>>30' is 00...0011 ` ` ` `System.out.println(x>>>` `31` `); ` `// The value of 'x>>>31' is 00...0001 ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

Output:

7 3 1

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the **Fundamentals of Java and Java Collections Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Left Shift and Right Shift Operators in C/C++
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Check if left and right shift of any string results into given string
- Bitwise operators in Java
- Operators in C | Set 2 (Relational and Logical Operators)
- Russian Peasant (Multiply two numbers using bitwise operators)
- Bitwise Operators in C/C++
- Check if a number is multiple of 9 using bitwise operators
- What are the differences between bitwise and logical AND operators in C/C++?
- Case conversion (Lower to Upper and Vice Versa) of a string using BitWise operators in C/C++
- Toggle case of a string using Bitwise Operators
- Check if a number is divisible by 17 using bitwise operators
- Check if a number is divisible by 8 using bitwise operators
- Check if a Number is Odd or Even using Bitwise Operators
- Generate first K multiples of N using Bitwise operators
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Count pairs with bitwise XOR exceeding bitwise AND from a given array