What are the bitwise zero fill right shift zero operators in Java?


This article will help you understand all about Bitwise Zero Fill Right Shift Zero Operators in Java. Note that Bitwise Zero Fill Right Shift Zero Operator is same as Bitwise Zero Fill Right Shift Operator. Before understanding right shift operators, let us revise about Operators.

OPERATORS

In computer programming we often need to perform some arithmetical or logical operations. In such circumstances, we need operators to perform these tasks. Thus, an Operator is basically a symbol or token, which performs arithmetical or logical operations and gives us meaningful result. The values involved in the operation are called Operands.

In this article we are discussing about one type of Bitwise Operators, so let’s jump into it.

BITWISE OPERATORS

OPERATORS MEANING
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
! Bitwise NOT
~ Bitwise complement
<< Bitwise Left shift
>> Bitwise Right shift
>>> Bitwise Zero fill right shift
<<= Bitwise Left shift assignment
>>= Bitwise Right shift assignment
>>>= Bitwise Zero fill right shift assignment

This article is about Bitwise Zero fill right shift operators, so let’s jump right into it.

Zero Fill Right Shift Operator

This operator shifts all bits towards the right by a certain number of specified bits. The difference between Right Shift and Zero fill Right Shift is Right Shift preserves the sign bit (i.e. if a = -8, then a >> 2 will be a negative number) whereas Zero fill Right Shift doesn’t consider the sign bit (i.e. if a = -8, then a >>> 2 will be a positive number).

You may wonder, how does zero fill right shift operator work when the number is negative? The answer is, if N is the number of bits to be right shifted, then excess bits are shifted off N places to the right and are discarded, while zero bits are shifted in N places from the left and are kept. The sign bit becomes 0, so the result is always positive.

Syntax Number >>> (no of places to be shifted).

For Example − We are going to perform a Zero fill Right shift on 1011 (11) by 1 bit.

Step 1 − Before Right Shift

1 0 1 1

Step 2 − After Right Shift

0 1 0 1 1

The rightmost bit is the discarded bit, whereas the leftmost bit is the replacement bit.

So, the new number is 0101 (5).

Let’s take a look at another example, we are going to perform a Zero fill Right shift on -5 (in binary 11111111111111111111111111111011) by 2 bits.

Step 1 − Before Right Shift

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1

Step 2 − After Right Shift, the last 2 bits (11) will be discarded and two zeros will be inserted on the left side, which are the replacement bits.

0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0

Now the new number is 1073741822, which is positive.

Example

Given below is the code where all the above examples will be displayed.

public class ZeroFillRightShiftOperator { // public class declaration public static void main(String[] args) { // main function declaration // initializing variables int a = 11; // Binary Number 01011 int b = -5; // Binary Number 11111111111111111111111111111011 // Example 1 where we Zero Fill Right Shifted Number 11 by 1 digit and got Number 5 as output int result_1 = a >>> 1; // Right Shifting 11 by 1 digit // Expected outcome 00101 = 5 System.out.println(" Zero Fill Right Shift of 11 by 1 digit is = " + result_1); // displaying result // Example 2 where we Zero Fill Right Shifted Number -5 by 2 digits and got Number 1073741822 as output int result_2 = b >>> 2; // Right Shifting -5 by 2 digits // Expected outcome 00111111111111111111111111111110 = 1073741822 System.out.println(" Zero Fill Right Shift of -5 by 2 digits is = " + result_2); // displaying result } }

Output

Zero Fill Right Shift of 11 by 1 digit is = 5
Zero Fill Right Shift of -5 by 2 digits is = 1073741822

Updated on: 05-Sep-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements