# Shift Operator in Java

• Last Updated : 26 Oct, 2021

Operators in Java are used to performing operations on variables and values.

Examples of operators: +, -, *,  /,  >>, <<.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Types of operators:

• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator.

In this article, we will mainly focus on the Shift Operators in Java.

By shifting the bits of its first operand right or left, a shift operator performs bit manipulation on data. The shift operators available in the Java programming language are listed below. The shift operator is a java operator that is used to shift bit patterns right or left.

### 1. SignedLeft Shift Operator in Java

This operator is represented by a symbol <<, read as double less than.

Syntax:

`left_operand  <<  number`

Description:

Calculate the value of number<<2 if number=2.

When the value of a number is shifted to the left two places, the leftmost two bits are lost. The number has a value of two. 0010 is the binary representation of the number 2. In the following example, the method for doing a left shift is explained:

In the example above, the binary number 0010 (in decimal 2) becomes 1000 after shifting the bits to the left (in decimal 8).

Example:

## Java

 `// Java program to demonstrate``// the Signed left shift operator``import` `java.io.*;`` ` `class` `GFG {``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `number = ``2``;``       ` `          ``// 2 bit left shift operation``        ``int` `Ans = number << ``2``; ``       ` `        ``System.out.println(Ans);``    ``}``}`
Output
`8`

### 2. Signed Right Shift Operator in Java

The Right Shift Operator moves the bits of a number a given number of places to the right. The >> sign represents the right shift operator, which is understood as double greater than. When you type x>>n, you tell the computer to move the bits x to the right n places.

When we shift a number to the right, the least significant bits (rightmost) are deleted, and the sign bit is filled in the most considerable place (leftmost).

Syntax:

`left_operand  >>  number`

Description:

Calculate the value of number>>2 if number=8.

When the value of a number is shifted to the right two places, the rightmost two bits are lost. The number has a value of eight. 1000 is the binary representation of the number 8. The following is an example of how to perform the right shift:

In the example above, the binary number 1000 (in decimal 8) becomes 0010 after shifting the bits to the right (in decimal 2).

Example:

## Java

 `// Java program to demonstrate``// the Signed right shift operator``import` `java.io.*;`` ` `class` `GFG ``    ``{``    ``public` `static` `void` `main (String[] args) {``    ``{         ``        ``int` `number = ``8``;``       ` `        ``// 2 bit signed right shift``        ``int` `Ans = number >> ``2``;``       ` `        ``System.out.println(Ans);    ``    ``}``}`
Output
`2`

### 3. Unsigned Right Shift Operator in Java

Unsigned Right Shift Operator moves the bits of the integer a given number of places to the right. The sign bit was filled with 0s. The Bitwise Zero Fill Right Shift Operator is represented by the symbol >>>.

Syntax:

`left_operand  >>>  number`

## Java

 `// Java program to demonstrate``// the Unsigned right shift operator``import` `java.io.*;`` ` `class` `GFG ``    ``{``    ``public` `static` `void` `main (String[] args) ``    ``{``        ``byte` `num1 = ``8``;``        ``byte` `num2 = -``8``;``      ` `        ``System.out.println(num1 >>> ``2``);    ``        ``System.out.println(num2 >>> ``2``);    ``    ``}``}`
Output
```2
1073741822```

Note: For negative bits, the signed and unsigned right shift operators provide different results.

### 4. Unsigned Left Shift Operator in Java

Unlike unsigned Right Shift, there is no “<<<” operator in Java because the logical (<<) and arithmetic left-shift (<<<) operations are identical.

My Personal Notes arrow_drop_up