Shift operators in java with example

Vern 129 Published: 12/13/2024

Shift operators in java with example

I'd be happy to help you with that!

Shift operators in Java are a type of bitwise operator used to shift the binary representation of a number left or right.

There are three main types of shift operators:

Left Shift Operator (<<): This operator shifts the bits of the number to the left and fills 0s on the right side. Right Shift Operator (>>): This operator shifts the bits of the number to the right and fills 0s or copies the sign bit on the left side. Zero-Extend Right Shift Operator (>>>)): This operator shifts the bits of the number to the right and fills 0s.

Here are some examples:

Left Shift Operator (<<)

public class LeftShiftExample {

public static void main(String[] args) {

int num = 10; // Binary representation: 1010

System.out.println("Initial Number: " + num);

// Perform left shift by 2 bits

num <<= 2;

System.out.println("Left Shifted Number: " + num);

}

}

Output:

Initial Number: 10

Left Shifted Number: 40

As you can see, the binary representation of num is shifted to the left by 2 bits, and 0s are filled on the right side. The decimal value becomes 40.

Right Shift Operator (>>)

public class RightShiftExample {

public static void main(String[] args) {

int num = 10; // Binary representation: 1010

System.out.println("Initial Number: " + num);

// Perform right shift by 2 bits

num >>= 2;

System.out.println("Right Shifted Number: " + num);

}

}

Output:

Initial Number: 10

Right Shifted Number: 1

The binary representation of num is shifted to the right by 2 bits, and 0s are filled or the sign bit on the left side. The decimal value becomes 1.

Zero-Extend Right Shift Operator (>>>))

public class ZeroExtendRightShiftExample {

public static void main(String[] args) {

int num = 10; // Binary representation: 1010

System.out.println("Initial Number: " + num);

// Perform zero-extend right shift by 2 bits

num >>>= 2;

System.out.println("Zero-Extend Right Shifted Number: " + num);

}

}

Output:

Initial Number: 10

Zero-Extend Right Shifted Number: -536870936

The binary representation of num is shifted to the right by 2 bits, and 0s are filled. The decimal value becomes a negative number.

In summary, the shift operators in Java allow you to manipulate the binary representation of a number, which can be useful for various programming tasks, such as data compression, encryption, or processing large integers.

Unsigned left shift operator in Java

The unsigned left shift operator (>>>) is a bitwise logical operator used in Java to perform a right shift operation on a given number. Unlike the signed left shift operator (<<), which shifts bits from the left and fills in with zeros, the unsigned left shift operator (>>>) shifts bits from the left, filling in with the most significant bit (i.e., the sign bit).

The syntax of the unsigned left shift operator is as follows:

value >>> number_of_positions_to_shift

Here, value is the original value that you want to shift, and number_of_positions_to_shift is the number of positions you want to shift it to the right.

When using the unsigned left shift operator (>>>), Java performs a bitwise logical operation on each bit of the value. The bits are shifted to the right by the specified number of positions, filling in with zeros. This means that the most significant bit (i.e., the sign bit) is replicated across all the new bits.

Here's an example to illustrate how this operator works:

int value = 16; // Binary: 10000

value >>> 1;

After shifting, value becomes:

8; // Binary: 1000

In this example, we start with the binary number 10000, which represents the decimal number 16. When we shift it to the right by one position (>>> 1), the leftmost bit (i.e., the sign bit) is replicated across all new bits. The resulting binary number is 1000, which represents the decimal number 8.

The unsigned left shift operator can be useful in various situations, such as:

When you need to align a value's most significant bit to a certain position. When you want to reduce the size of a data type by shifting its bits to the right.

However, it is essential to note that when using the unsigned left shift operator, the resulting value may not be what you expect if you are not careful. This is because the sign bit (i.e., the most significant bit) can affect how the bits are filled in during the shifting process.

For instance:

int value = Integer.MIN_VALUE; // -2147483648

value >>> 31;

After shifting, value becomes:

0; // Binary: 00000000000000000000000000000000

In this case, we start with the minimum integer value (-2,147,483,648) and shift it to the right by 31 positions (>>> 31). The resulting value is 0 because the leftmost bit (i.e., the sign bit) is filled in with zeros.

As you can see, using the unsigned left shift operator (>>>) without caution can lead to unexpected results. Therefore, it's crucial to carefully consider your specific use case and the potential implications of shifting your data.