Shift Operator in Java
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
|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.
left_operand << number
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:
In the below example below, the binary number 0010 (in decimal 2) becomes 1000 after shifting the bits to the left (in decimal 8).
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).
left_operand >> number
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).
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 >>>.
left_operand >>> number
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.
Please Login to comment...