Java Integer rotateRight() Method

The Integer.rotateRight() method in Java is used to rotate the bits of the specified integer value to the right by a given distance.

Table of Contents

  1. Introduction
  2. rotateRight() Method Syntax
  3. Examples
    • Rotating Bits of a Positive Integer
    • Rotating Bits of a Negative Integer
    • Rotating Bits with Distance Greater Than 32
  4. Real-World Use Case
  5. Conclusion

Introduction

The Integer.rotateRight() method is a static method in the Integer class in Java. It rotates the bits of the specified integer value to the right by the specified distance. The bits that are shifted out from the right end are reintroduced at the left end. This method is useful for various bit manipulation tasks, such as in cryptography, hashing algorithms, and computer graphics.

rotateRight()() Method Syntax

The syntax for the Integer.rotateRight() method is as follows:

public static int rotateRight(int i, int distance)
  • i: The integer value whose bits are to be rotated.
  • distance: The number of positions to rotate the bits to the right.

The method returns:

  • An integer whose bits are the result of rotating the bits of the specified integer to the right by the specified distance.

Examples

Rotating Bits of a Positive Integer

The rotateRight() method can be used to rotate the bits of a positive integer to the right.

Example

public class RotateRightExample {
    public static void main(String[] args) {
        int number = 13; // Binary representation: 00000000 00000000 00000000 00001101
        int distance = 2;
        int rotated = Integer.rotateRight(number, distance);

        System.out.println("Original binary: " + Integer.toBinaryString(number));
        System.out.println("Rotated binary: " + Integer.toBinaryString(rotated));
        System.out.println("Rotated integer: " + rotated);
    }
}

Output:

Original binary: 1101
Rotated binary: 10000000000000000000000000000011
Rotated integer: 1073741827

In this example, the bits of the integer 13 are rotated to the right by 2 positions, resulting in the integer 1073741827.

Rotating Bits of a Negative Integer

The rotateRight() method can also be used to rotate the bits of a negative integer to the right.

Example

public class RotateRightNegativeExample {
    public static void main(String[] args) {
        int number = -13; // Binary representation: 11111111 11111111 11111111 11110011
        int distance = 3;
        int rotated = Integer.rotateRight(number, distance);

        System.out.println("Original binary: " + Integer.toBinaryString(number));
        System.out.println("Rotated binary: " + Integer.toBinaryString(rotated));
        System.out.println("Rotated integer: " + rotated);
    }
}

Output:

Original binary: 11111111111111111111111111110011
Rotated binary: 11111101111111111111111111111111
Rotated integer: -1610612737

In this example, the bits of the integer -13 are rotated to the right by 3 positions, resulting in the integer -1610612737.

Rotating Bits with Distance Greater Than 32

If the distance is greater than 32, the method effectively performs the rotation by distance % 32 positions, as the integer is a 32-bit value.

Example

public class RotateRightLargeDistanceExample {
    public static void main(String[] args) {
        int number = 13; // Binary representation: 00000000 00000000 00000000 00001101
        int distance = 34; // Equivalent to rotating by 34 % 32 = 2 positions
        int rotated = Integer.rotateRight(number, distance);

        System.out.println("Original binary: " + Integer.toBinaryString(number));
        System.out.println("Rotated binary: " + Integer.toBinaryString(rotated));
        System.out.println("Rotated integer: " + rotated);
    }
}

Output:

Original binary: 1101
Rotated binary: 10000000000000000000000000000011
Rotated integer: 1073741827

In this example, the bits of the integer 13 are rotated to the right by 34 positions, which is equivalent to rotating by 2 positions, resulting in the integer 1073741827.

Real-World Use Case

Bitwise Operations in Cryptography

In a real-world application, you might use the Integer.rotateRight() method to perform bitwise operations in cryptographic algorithms, such as rotating bits for data encryption or hashing.

Example

public class CryptographyExample {
    public static void main(String[] args) {
        int key = 0b10101010101010101010101010101010; // Example key
        int rotatedKey = Integer.rotateRight(key, 5);

        System.out.println("Original key binary: " + Integer.toBinaryString(key));
        System.out.println("Rotated key binary: " + Integer.toBinaryString(rotatedKey));
        System.out.println("Rotated key integer: " + rotatedKey);
    }
}

Output:

Original key binary: 10101010101010101010101010101010
Rotated key binary: 1010101010101010101010101010
Rotated key integer: 876536712

In this example, the bits of the key are rotated to the right by 5 positions for use in a cryptographic algorithm, resulting in the integer 876536712.

Conclusion

The Integer.rotateRight() method in Java is a powerful and useful tool for rotating the bits of an integer to the right by a specified distance. By understanding how to use this method, you can efficiently handle tasks that involve bit manipulation in your Java applications. Whether you are dealing with positive or negative integers, or implementing cryptographic algorithms, the rotateRight() method provides a reliable solution for these tasks.

Comments