{"record":{"id":"38909312328f5ef7","repo":"TheAlgorithms/Java","slug":"shift-amount-cannot-be-negative","errorCode":null,"errorMessage":"Shift amount cannot be negative: {}","messagePattern":"Shift amount cannot be negative: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/bitmanipulation/BitRotate.java","lineNumber":40,"sourceCode":"    }\n\n    /**\n     * Performs a circular left rotation (left shift) on a 32-bit integer.\n     * Bits shifted out from the left side are inserted on the right side.\n     *\n     * @param value the 32-bit integer value to rotate\n     * @param shift the number of positions to rotate left (must be non-negative)\n     * @return the result of left rotating the value by the specified shift amount\n     * @throws IllegalArgumentException if shift is negative\n     *\n     * @example\n     * // Binary: 10000000 00000000 00000000 00000001\n     * rotateLeft(0x80000001, 1)\n     * // Returns: 3 (binary: 00000000 00000000 00000000 00000011)\n     */\n    public static int rotateLeft(int value, int shift) {\n        if (shift < 0) {\n            throw new IllegalArgumentException(\"Shift amount cannot be negative: \" + shift);\n        }\n\n        // Normalize shift to the range [0, 31] using modulo 32\n        shift = shift % 32;\n\n        if (shift == 0) {\n            return value;\n        }\n\n        // Left rotation: (value << shift) | (value >>> (32 - shift))\n        return (value << shift) | (value >>> (32 - shift));\n    }\n\n    /**\n     * Performs a circular right rotation (right shift) on a 32-bit integer.\n     * Bits shifted out from the right side are inserted on the left side.\n     *\n     * @param value the 32-bit integer value to rotate","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/bitmanipulation/BitRotate.java#L22-L58","documentation":"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].","triggerScenarios":"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).","commonSituations":"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.","solutions":["Pass a non-negative shift; if you need a right rotation, call rotateRight instead of passing a negative value to rotateLeft.","Validate the shift source and reject/clamp negatives before calling.","If the shift can be negative by design, branch: negative -> rotateRight(value, -shift)."],"exampleFix":"// before\nBitRotate.rotateLeft(value, delta);  // throws if delta < 0\n\n// after\nint s = delta < 0 ? BitRotate.rotateRight(value, -delta)\n                 : BitRotate.rotateLeft(value, delta);","handlingStrategy":"validation","validationCode":"if (shift < 0) throw new IllegalArgumentException(\"shift must be non-negative\");\nBitRotate.rotateLeft(value, shift);","typeGuard":"public static boolean isValidShift(int shift) {\n    return shift >= 0;\n}","tryCatchPattern":"try {\n    return BitRotate.rotateLeft(value, shift);\n} catch (IllegalArgumentException e) {\n    return BitRotate.rotateRight(value, -shift);\n}","preventionTips":["Use rotateRight for the opposite direction instead of negative shifts.","Clamp or branch on negative shifts at the call site.","Validate user-supplied shift amounts."],"tags":["bit-manipulation","rotate","argument-validation","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}