×

Shift right zero Fill Operator in Java and Operator Shifting

Left shift operator ( << ) :

The left shift operator is an operator which performs its action at the bits level of a binary Operator. That means when you perform a bitwise left Shift on any operator, and it shifts the position of that particular bit to its next higher bit representation in binary order.

It is denoted with as “ << ”.

If  X<< 1 is applied on an operator, then the bits shifted only once to the left. If X << 2 is applied, then the bit gets shifted twice to the left side. Similarly , if X<< n is applied , then the bit gets shifted n times to its left.

For example ,

Consider 7 << 2 ( read as seven left Shift of 2 )

Shifting the value of X to the left two positions results in the loss of the leftmost two bits. X is equal to 7. The binary equivalent of 7 is 00000111. The following example demonstrates how to perform a left shift :

In the preceding example, the binary number 00000111 ( in decimal 7 ) becomes 00011100 after shifting the bits to the left ( in decimal 28 ). This is how the left shift operator works on numbers.

Bitwise right shift ( << ) :

The Right Shift Operator adjusts the bits of an integer to the right by a specified n place. The right shift operator is represented by the symbol “ >> ” , which stands for twofold larger than. The definition of x >> n is to shift the bits x to the right n specified places.

“ >> ” transfers the bits to the right while retaining the sign bit, which is the leftmost bit. The number's sign is represented by the leftmost bit. The sign bit 0 denotes a positive number, while the sign bit 1 denotes a negative number. So, when we perform “ >> ” on a positive value, we likewise get a positive value as a result. When we use “ >> ” on a negative sign, we receive another negative value.

If  X >> 1 is applied on an operator, then the bits shift only once to the right. If X >> 2 is applied , then the bit gets shifted twice to the right side. Similarly , if X >> n is applied , then the bit gets shifted n times to its right.

For example ,

Consider 7 >> 2 ( read as 7 right shift of 2 )

Shifting the value of X to the right two positions results in the loss of the rightmost two bits. X is equal to 7. The binary equivalent of 7 is 00000111. The following example demonstrates how to perform a right shift:

In the preceding example, the binary number 00000111 ( in decimal 7 ) becomes 00000001 after shifting the bits to the left ( in decimal 1 ). This is how the right shift operator works on numbers.

Bitwise Zero Fill Right Shift Operator ( >>> ) :

The zero-fill right shift operator (>>>) shifts the binary representation of the left-hand operand by the number of bits given by the second operand, modulo 32. It examines the left-hand operand as just an unsigned integer.

Excess bits are thrown to the right, while zero bits are moved in from the left. The outcome is always positive because the sign bit is set to zero. Unlike the other bitwise operators, zero-fill right shift produces an unsigned 32-bit integer. The Bitwise Zero Fill Right Shift Operator moves an integer's bits n places to the right. There were zeros in the sign bit. The letter ">>" stands for the Bitwise Zero Fill Right Shift Operator.

When we use “ >>> ” on a positive number, we get the same result as “ >> ” . When we apply “ >>> ” to a negative number, it produces a positive result. MSB is substituted with a 0.

This Operator moves every bit by the specified number of bits to the right. What makes Right Shift & Zero Fill different from one another? Right Shift preserves the sign bit (i.e., a >> 2 will become a negative value if a = -8), whereas Zero fill does not. Right Shift doesn't (i.e., a >> 2 becomes a positive value if a = -8).

You might be wondering how a zero fill right shift operator works if the integer is negative. If N is the number of bits to be right shifted, excess bits are relocated N places to the right and discarded, while zero bits are transferred N places to the left and retained. Because the sign bit is set to zero, the outcome is always positive.

In the above example , the binary number 00100000 ( in decimal 32 ) becomes 00000100 once the bits are shifted to the right ( in decimal 4 ). The final three bits moved out and were lost.

Difference between right shift ( >> ) and zero fill right shift ( >>> ) :

“ >> ” and “ >>> ” are both used to shift the bits to the right . The distinction is that the operation >> preserves the sign bit , but the operator “ >>> ” does not. To keep the sign bit , you must add 0 to the MSB.

Main.java

public class Main
{
public static void main ( String [ ] args ) {
byte a,  b ;  
a = 7 ;  
b = -7 ;  
System.out.println ( " Left Shift: a<<2 = " + ( a << 2 ) ) ;  
System.out.println ( " Right Shift: b>>2 = "+ ( b >> 2 ) ) ;  
System.out.println ( " Zero Fill Right Shift: a >>> 2  = " + ( a >>> 2 ) ) ;  
System.out.println ( " Zero Fill Right Shift: b >>> 2 = " + ( b >>> 2 ) ) ;  
}
}

Output :

Shift right zero fill operator in Java and Operator shifting

Related Topics

Finding Odd Occurrence of a Number in Java

In this tutorial, we will learn how to find the odd occurrence of a number through a java program. Let us consider an array of non-negative integers. Here, except for one...

6 minutes read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Java Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

4 minutes read.

Java SHA256

Definition: In cryptography, SHA is a hash function that takes 20 bytes of input and produces an approximate 40-digit hexadecimal integer as the hash result. Class for Message Digest: Java's MessageDigest Class,...

2 minutes read.

Java String vs StringBuffer

Java String vs StringBuffer In this section, we will discuss the key differences between String and StringBuffer class. Before moving to the ahead in this section, let’s introduce with both classes. String...

4 minutes read.

Java Get Time in UTC

UTC is the abbreviation for Universal Time Coordinated. Before the beginning of UTC, it is mentioned as the Greenwich Mean Time (GMT) but Now it is mentioned as the universal...

4 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.

Java concurrency interview questions

During technical interviews, one of the most challenging and sophisticated subjects is concurrency in Java. This page offers responses to some of the related interview questions you might come across. 1....

11 minutes read.

Zygodromes in Java

Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg"...

4 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Magic Square in Java

A square with numbers is referred to as a magic square. The numbers in a magic square of order n are arranged to ensure that the sum of all of...

4 minutes read.

public static void main string args meaning in java

In java main() method is the initial point for execution of the program. If a program doesn’t contain the main method, the program will not execute. JVM(java virtual machine) starts...

3 minutes read.

Java md5 Hash Example

A 128-bit hash value is generated by the Message Digest Algorithm 5, which is a cryptographic algorithm. A stationary hash value is generated by the hash function from data of...

3 minutes read.

Exception Handling in Java

Before looking at how exceptions are handled in java, it is necessary to see what an exception is. What is Exception? Whenever a program is written, errors are encountered. Some of these...

6 minutes read.

Sales Tax Problem in Java

To calculate the sales tax on a purchase in Java, you will need to know the following information: The cost of the item being purchased The sales tax rate You can then use...

4 minutes read.

Zig Zag star and Number Pattern in Java

We covered many Java pattern applications in the preceding part. We will write Java applications for zigzag star and number patterns in this part. Printing Zig Zag Number Pattern Steps Print one...

3 minutes read.

How to Update Java

As we all know that java can be installed in all operating systems like windows, Linux, macOS. We are available with the java 17 and java 18 versions in the...

3 minutes read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.