{"record":{"id":"513b85a8e64190d7","repo":"TheAlgorithms/Java","slug":"the-exponent-must-be-positive","errorCode":null,"errorMessage":"The exponent must be positive","messagePattern":"The exponent must be positive","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwo.java","lineNumber":23,"sourceCode":" * of a number when divided by a power of two (2^n)\n * without using division or modulo operations.\n *\n * @author Hardvan\n */\npublic final class ModuloPowerOfTwo {\n    private ModuloPowerOfTwo() {\n    }\n\n    /**\n     * Computes the remainder of a given integer when divided by 2^n.\n     *\n     * @param x the input number\n     * @param n the exponent (power of two)\n     * @return the remainder of x divided by 2^n\n     */\n    public static int moduloPowerOfTwo(int x, int n) {\n        if (n <= 0) {\n            throw new IllegalArgumentException(\"The exponent must be positive\");\n        }\n\n        return x & ((1 << n) - 1);\n    }\n}\n","sourceCodeStart":5,"sourceCodeEnd":29,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwo.java#L5-L29","documentation":"Thrown by ModuloPowerOfTwo.moduloPowerOfTwo(int x, int n) when n <= 0. The method computes x mod 2^n via the bit trick x & ((1 << n) - 1). For n = 0 the mask degenerates to 0 (always-yielding 0, not the intended modulo), and for n < 0 the shift is undefined for this trick, so the library requires a strictly positive exponent.","triggerScenarios":"Calling moduloPowerOfTwo(x, n) with n == 0 or n < 0. Common when n is computed as log2 of a size and the size is 1 (giving 0) or when an uninitialized/default int (0) is passed.","commonSituations":"Deriving the exponent from an array/buffer size via bit-length where the size is a power of two equal to 1; config value left at default 0; off-by-one in computing power from a divisor.","solutions":["Ensure the exponent is at least 1 before calling (n >= 1).","Recompute the exponent source so it cannot reach 0 (e.g. require size >= 2).","If you need modulo by 1 (result always 0), short-circuit that case explicitly before calling."],"exampleFix":"// before\nint r = ModuloPowerOfTwo.moduloPowerOfTwo(x, log2(size));\n\n// after\nint exp = Integer.numberOfTrailingZeros(size);\nint r = exp > 0 ? ModuloPowerOfTwo.moduloPowerOfTwo(x, exp) : 0;","handlingStrategy":"validation","validationCode":"if (n <= 0) {\n    throw new IllegalArgumentException(\"exponent n must be >= 1\");\n}\nint r = ModuloPowerOfTwo.moduloPowerOfTwo(x, n);","typeGuard":"static boolean validExponent(int n) { return n >= 1; }","tryCatchPattern":"try {\n    int r = ModuloPowerOfTwo.moduloPowerOfTwo(x, n);\n} catch (IllegalArgumentException e) {\n    // n was <= 0; fall back to x mod 1 == 0 only if that is intended\n}","preventionTips":["When deriving n from a power-of-two size, ensure the size is >= 2.","Reject size == 1 upstream since its exponent is 0.","Default exponent config to a safe positive value, never 0."],"tags":["bit-manipulation","java","validation","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}