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.
Types of Shift Operators in Java:
Name of operator
| Sign | Description |
---|
Signed Left Shift | << | The left shift operator moves all bits by a given number of bits to the left. |
Signed Right Shift | >> | The right shift operator moves all bits by a given number of bits to the right. |
Unsigned Right Shift | >>> | It is the same as the signed right shift, but the vacant leftmost position is filled with 0 instead of the sign bit. |
1. Signed Left Shift Operator in Java
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 classes
import java.io.*;
// Main class
class 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);
}
}
2. Signed Right Shift Operator in Java
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 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);
}
}
}
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]);
}
}
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);
}
}
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.
Similar Reads
Left Shift Operator in Java
The decimal representation of a number is a base-10 number system having only ten states 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. For example, 4, 10, 16, etc. The Binary representation of a number is a base-2 number system having only two states 0 and 1. For example, the binary representation of 4, a base-
4 min read
Java Operators
Java operators are special symbols that perform operations on variables or values. These operators are essential in programming as they allow you to manipulate data efficiently. They can be classified into different categories based on their functionality. In this article, we will explore different
15 min read
Basic Operators in Java
Java provides a rich operator environment. We can classify the basic operators in java in the following groups: Arithmetic OperatorsRelational OperatorsBitwise OperatorsAssignment OperatorsLogical Operators Let us now learn about each of these operators in detail. 1. Arithmetic Operators: Arithmetic
10 min read
Bitwise Right Shift Operators in Java
In C/C++ there is only one right shift operator '>>' which should be used only for positive integers or unsigned integers. Use of the right shift operator for negative numbers is not recommended in C/C++, and when used for negative numbers, the output is compiler dependent. Unlike C++, Java su
2 min read
Typecasting in Java
Typecasting in Java is the process of converting one data type to another data type using the casting operator. When you assign a value from one primitive data type to another type, this is known as type casting. To enable the use of a variable in a specific manner, this method requires explicitly i
4 min read
Java Assignment Operators with Examples
Operators constitute the basic building block of any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
7 min read
BigInteger shiftLeft() Method in Java
The java.math.BigInteger.shiftLeft(int n) method returns a BigInteger whose value is (this << n). The shift distance, n, may be negative, in which case this method performs a right shift.shiftLeft() method will moves each digit in a number's binary representation left by n times and the last b
2 min read
Right Shift Operator (>>) in Programming
Right shift operator (>>), commonly found in programming languages, including C, C++, Java, and others, is used to shift the bits of a number to the right by a specified number of positions. Each shift moves all bits in the operand to the right by the number of positions indicated by the right
15+ min read
Switch Statements in Java
The switch statement in Java is a multi-way branch statement. In simple words, the Java switch statement executes one statement from multiple conditions.It is an alternative to an if-else-if ladder statement. It provides an easy way to dispatch execution to different parts of the code based on the v
9 min read
Vector set() Method in Java
The Java.util.Vector.set() method is used to replace any particular element in the vector, created using the Vector class, with another element. Syntax: Vector.set(int index, Object element) Parameters: This function accepts two mandatory parameters as shown in the above syntax and described below.
2 min read