Skip to content

[FEATURE REQUEST] Rotate Bits [Left \ Right] #6967

@AmandeepMandal1077

Description

@AmandeepMandal1077

What would you like to Propose?

Add a new bit-manipulation utility that implements circular bit-rotations (rotate left and rotate right) for 32-bit integers.

Proposed files:

  • BitRotate.java — contains rotateLeft(int value, int shift) and rotateRight(int value, int shift).
  • BitRotateTest.java — JUnit tests covering typical, boundary, and edge cases. Tests only for 32-bit behavior.

Rationale:

  • Circular rotations are a common low-level operation used in cryptography, hashing, compression and performance-sensitive code.

Issue details

Problem statement

Implement two functions for 32-bit integers:

  • rotateLeft(int value, int shift)
  • rotateRight(int value, int shift)

Behavior & requirements

  • Rotations are circular (bits shifted out on one side re-enter on the other).
  • Only 32-bit semantics must be supported (i.e., treat input as unsigned 32-bit value).
  • Shifts should be normalized modulo 32 (so a shift of 32 == 0).
  • Negative shift values should either be prohibited (throw IllegalArgumentException) or normalized (document choice). Preferred: treat negative as illegal and document behavior.
  • Time complexity: O(1).
  • No external dependencies.

API suggestion

Public static methods in BitRotate:

public final class BitRotate {
    private BitRotate() { /* hide constructor */ }

    public static int rotateLeft(int value, int shift) { ... }

    public static int rotateRight(int value, int shift) { ... }
}

### Additional Information

_No response_

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions