Binary Right Shift Zero Fill Assign Operator

Binary Right Shift Operation is like dividing a number by a power of two while preserving sign. Binary Right Shift Zero Fill is similar, where we are dividing a number by a power of 2, but unsigned. Which means we lose the sign of the original number after shifting, because we are replacing the digits shifted to the right by 0. Which turns the result into a positive number. This post will give examples on how to use Binary Right Shift Zero Fill Assign Operator in the Java Programming Language.

Binary Right Shift Zero Fill Assign With Literal

The operator denoted by >>>= will perform binary right shift zero fill assign operation. The difference between binary right shift and binary right shift zero fill is that the former is signed, while the latter is unsigned. But the result is similar if we are performing it on positive numbers. For example:
int a = 128;
a >>>= 4;
System.out.println(a);
So we are dividing 128 by 2 to the power of 4, which is 128 divided by 16, resulting to:
8
And if the number is negative value, for example:
int a = -128;
a >>>= 4;
System.out.println(a);
We are shifting -128 in binary by 4 digits to the right. The digits shifted coming from the left are replaced by 0, giving the result below:
268435448
The sign of the number becomes part of the value of the number, which gives us a very large number in this example:

Binary Right Shift Zero Fill Assign With Another variable

Below is an example of using Binary Right Shift Zero Fill Assign with variable on the right side:

int a = 55;
int b = 3;
a >>>= b;
System.out.println(a);

So this becomes 55 divided by 2 to the power of 3, which is 55 divided by 8, this results to below output:
6

Binary Right Shift Zero Fill Assign With An Expression

And we can always use complex expression on the right side, for example:
int a = 49;
int b = 2;
a >>>= b + 2;
System.out.println(a);
So b + 2 is 4, and we divide 49 by 16. Which gives us the output below:
3