{"record":{"id":"38346d1c18b956a8","repo":"apache/flink","slug":"the-given-value-is-not-a-power-of-two","errorCode":null,"errorMessage":"The given value {} is not a power of two.","messagePattern":"The given value (.+?) is not a power of two\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/util/MathUtils.java","lineNumber":56,"sourceCode":"\n        return 31 - Integer.numberOfLeadingZeros(value);\n    }\n\n    /**\n     * Computes the logarithm of the given value to the base of 2. This method throws an error, if\n     * the given argument is not a power of 2.\n     *\n     * @param value The value to compute the logarithm for.\n     * @return The logarithm to the base of 2.\n     * @throws ArithmeticException Thrown, if the given value is zero.\n     * @throws IllegalArgumentException Thrown, if the given value is not a power of two.\n     */\n    public static int log2strict(int value) throws ArithmeticException, IllegalArgumentException {\n        if (value == 0) {\n            throw new ArithmeticException(\"Logarithm of zero is undefined.\");\n        }\n        if ((value & (value - 1)) != 0) {\n            throw new IllegalArgumentException(\n                    \"The given value \" + value + \" is not a power of two.\");\n        }\n        return 31 - Integer.numberOfLeadingZeros(value);\n    }\n\n    /**\n     * Decrements the given number down to the closest power of two. If the argument is a power of\n     * two, it remains unchanged.\n     *\n     * @param value The value to round down.\n     * @return The closest value that is a power of two and less or equal than the given value.\n     */\n    public static int roundDownToPowerOf2(int value) {\n        return Integer.highestOneBit(value);\n    }\n\n    /**\n     * Casts the given value to a 32 bit integer, if it can be safely done. If the cast would change","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/util/MathUtils.java#L38-L74","documentation":"The second guard in MathUtils.log2strict: after ruling out 0, the bit trick (value & (value - 1)) != 0 detects that value is not an exact power of two, and IllegalArgumentException is thrown naming the value. log2strict exists specifically for sizing math (segments, hash tables) that requires power-of-two dimensions.","triggerScenarios":"Calling log2strict(v) with v in {3, 5, 6, 7, 9, ...} — any positive value that is not 1, 2, 4, 8, 16, ... Typical sources: user-supplied buffer sizes, segment counts, or parallelism values that bypassed power-of-two validation.","commonSituations":"Memory segment size or table capacity configured to a round-but-non-power-of-two number like 1000 or 3000000; config parsing that accepts arbitrary ints where the code later requires 2^k; refactors that replaced a rounding step (roundDownToPowerOf2) with a direct pass-through.","solutions":["Round the input first: use MathUtils.roundDownToPowerOf2(value) (or enforce/round up) before log2strict.","Fix the configuration to a power of two (e.g. 33554432 instead of 32000000 for a 32MB-ish segment).","Add config validation that rejects non-power-of-two values early with a message naming the option key.","If arbitrary sizes must be supported, switch the consumer code from log2strict/indexing to non-power-of-two-safe arithmetic."],"exampleFix":"// before\nint segBits = MathUtils.log2strict(segmentSize); // segmentSize = 1000 -> IAE\n\n// after\nint segSizePow2 = MathUtils.roundDownToPowerOf2(segmentSize);\nint segBits = MathUtils.log2strict(segSizePow2);","handlingStrategy":"validation","validationCode":"if (value <= 0 || (value & (value - 1)) != 0) {\n    value = MathUtils.roundDownToPowerOf2(value); // or reject\n}\nint log = MathUtils.log2strict(Math.max(1, value));","typeGuard":"// Java has no type guard; use a boolean predicate\nstatic boolean isPowerOfTwoSafe(int v) { return v > 0 && (v & (v - 1)) == 0; }","tryCatchPattern":null,"preventionTips":["Round sizes to powers of two before log2strict","Use MathUtils.isPowerOf2 to validate config values early","Prefer power-of-two values in size-related options (e.g. 2^25 not 32000000)"],"tags":["math","logarithm","power-of-two","configuration","sizing"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}