Shift Operator in Java

24 May, 2022
Operators in Java are used to performing operations on variables and values.  
Examples of operators: +, -, *,  /,  >>, <<.
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.
Name of operator
This operator is represented by a symbol <<, read as double less than. 
Syntax: 
left_operand  <<  number
Illustration:
Java
//  Left Shifting a byte value class GFG {    public static void main(String[] args)    {        byte a = 64, b;        int i;         i = a << 2;        b = (byte)(a << 2);        System.out.println("Original value of a: " + a);        System.out.println("i and b: " + i + " " + b);    }}
Calculating 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:
Example: 
In the below example below, the binary number 0010 (in decimal 2) becomes 1000 after shifting the bits to the left (in decimal 8).
Java
// Java Program to demonstrate// Signed Left-Shift Operator // Importing required classesimport java.io.*; // Main classclass GFG {     // main driver method    public static void main(String[] args)    {        int number = 2;         // 2 bit left shift operation        int Ans = number << 2;         System.out.println(Ans);    }}
8
The Right Shift Operator moves the bits of a number in 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
Illustration:
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 operatorimport 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);       }}
Java
// Masking sign extension  class GFG {    public static void main (String[] args) {       char hex[]={       '0','1','2','3','4','5',         '6','7','8','9','a','b','c',         'd','e','f'       };             byte b=(byte) 0xf1;             System.out.println("b = 0x" + hex [(b>>4) & 0x0f] + hex[b & 0x0f]);    }}
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 operatorimport 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);       }}
2
1073741822
Note: For negative bits, the signed and unsigned right shift operators provide different results.
Unlike unsigned Right Shift, there is no “<<<” operator in Java because the logical (<<) and arithmetic left-shift (<<<) operations are identical.
singhankitasingh066
Java-Operators
Picked
Java
Java-Operators
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Abstraction in Java
HashSet in Java