TheAlgorithms/Java · error · IllegalArgumentException

Shift amount cannot be negative: {}

Error message

Shift amount cannot be negative: {}

What it means

Thrown by BitRotate.rotateLeft(value, shift) when shift is negative. Rotation by a negative amount is ambiguous for the bitwise rotate primitive, and the implementation explicitly normalizes shift with modulo 32 only after confirming non-negativity. The caller must supply a non-negative shift; values >= 32 are folded into [0,31].

Source

Thrown at src/main/java/com/thealgorithms/bitmanipulation/BitRotate.java:40

    }

    /**
     * Performs a circular left rotation (left shift) on a 32-bit integer.
     * Bits shifted out from the left side are inserted on the right side.
     *
     * @param value the 32-bit integer value to rotate
     * @param shift the number of positions to rotate left (must be non-negative)
     * @return the result of left rotating the value by the specified shift amount
     * @throws IllegalArgumentException if shift is negative
     *
     * @example
     * // Binary: 10000000 00000000 00000000 00000001
     * rotateLeft(0x80000001, 1)
     * // Returns: 3 (binary: 00000000 00000000 00000000 00000011)
     */
    public static int rotateLeft(int value, int shift) {
        if (shift < 0) {
            throw new IllegalArgumentException("Shift amount cannot be negative: " + shift);
        }

        // Normalize shift to the range [0, 31] using modulo 32
        shift = shift % 32;

        if (shift == 0) {
            return value;
        }

        // Left rotation: (value << shift) | (value >>> (32 - shift))
        return (value << shift) | (value >>> (32 - shift));
    }

    /**
     * Performs a circular right rotation (right shift) on a 32-bit integer.
     * Bits shifted out from the right side are inserted on the left side.
     *
     * @param value the 32-bit integer value to rotate

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass a non-negative shift; if you need a right rotation, call rotateRight instead of passing a negative value to rotateLeft.
  2. Validate the shift source and reject/clamp negatives before calling.
  3. If the shift can be negative by design, branch: negative -> rotateRight(value, -shift).

Example fix

// before
BitRotate.rotateLeft(value, delta);  // throws if delta < 0

// after
int s = delta < 0 ? BitRotate.rotateRight(value, -delta)
                 : BitRotate.rotateLeft(value, delta);
Defensive patterns

Strategy: validation

Validate before calling

if (shift < 0) throw new IllegalArgumentException("shift must be non-negative");
BitRotate.rotateLeft(value, shift);

Type guard

public static boolean isValidShift(int shift) {
    return shift >= 0;
}

Try / catch

try {
    return BitRotate.rotateLeft(value, shift);
} catch (IllegalArgumentException e) {
    return BitRotate.rotateRight(value, -shift);
}

Prevention

When it happens

Trigger: Calling `rotateLeft(x, -1)` or `rotateLeft(x, -8)`. The guard `shift < 0` triggers before the modulo normalization. Any shift >= 0 is accepted (including large values, which are reduced mod 32).

Common situations: Shift computed from a subtraction that goes negative; shift read from user/CLI input without validation; mixing left/right rotate direction by sign where the caller expected rotateRight for negatives.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/38909312328f5ef7. Report an issue: GitHub.