Javatpoint Logo
Javatpoint Logo

Triple Shift Operator in Java

In Java, the triple shift operator is represented by ">>>" and is a bit manipulation operator that shifts the bits of a given value to the right by a specified number of positions, filling the leftmost bits with zeros. Unlike the double shift operator ">>" which fills the leftmost bits with the sign bit of the original value, the triple shift operator always fills the leftmost bits with zeros.

The syntax of the triple shift operator is as follows:

where value is the value whose bits are to be shifted, numBits is the number of bit positions to shift, and result is the result of the shift operation.

The triple shift operator is particularly useful in situations where we want to perform logical right shifts on unsigned integer values. In Java, integer values are represented using two's complement notation, which means that the leftmost bit is used as a sign bit. This makes it difficult to perform logical right shifts on unsigned integer values, as the sign bit will be shifted into the rightmost bit position, effectively changing the value of the integer.

For example, consider the following code:

In this case, x is initially set to -1, which in binary representation is 11111111 11111111 11111111 11111111. When we perform a right shift by 1 bit position using the double shift operator, we get 11111111 11111111 11111111 11111111, which is still -1. This is because the sign bit is shifted into the rightmost bit position, preserving the negative value of the integer.

However, if we use the triple shift operator instead, we get a different result:

In this case, the value of x after the shift operation is 01111111 11111111 11111111 11111111, which is 2147483647 in decimal notation. This is because the leftmost bit is filled with a 0 instead of a 1, effectively changing the value of the integer to a positive value.

In addition to performing logical right shifts on unsigned integer values, the triple shift operator can also be used to optimize certain bit manipulation operations, such as dividing a signed integer by a power of 2.

The triple shift operator in Java is a useful bit manipulation operator that allows us to perform logical right shifts on unsigned integer values and optimize certain bit manipulation operations. However, it should be used with care, as it can also introduce unexpected behaviour when used improperly.

Here's an example program that demonstrates the use of the triple shift operator in Java:

TripleShiftExample.java

Output:

x: -1
y: 2147483647

In this program, we declare an integer variable x and initialize it to -1, which in binary representation is 11111111 11111111 11111111 11111111. We then use the triple shift operator to perform a logical right shift of x by 1 bit position, storing the result in a new integer variable y.

Finally, we print out the values of x and y using the System.out.println() method. As we can see from the output, the value of x remains -1 after the shift operation, while the value of y is now 2147483647. This is because the triple shift operator filled the leftmost bit of x with a 0, effectively changing its value to a positive integer.

Overall, this example program demonstrates how the triple shift operator can be used in Java to perform logical right shifts on unsigned integer values and optimize certain bit manipulation operations.

One important thing to note about the triple shift operator is that it has a lower precedence than the arithmetic, logical, and bitwise operators in Java. This means that it is evaluated after those operators, and we need to use parentheses to control the order of evaluation if needed.

For example, consider the following code:

TripleShiftExample.java

Output:

y: 17
z: 2






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA