{"record":{"id":"389ac9459958f4d3","repo":"TheAlgorithms/Java","slug":"absolute-value-of-long-min-value-does-not-fit-into","errorCode":null,"errorMessage":"Absolute value of Long.MIN_VALUE does not fit into signed long. Use gcdBig() for full-range support.","messagePattern":"Absolute value of Long\\.MIN_VALUE does not fit into signed long\\. Use gcdBig\\(\\) for full-range support\\.","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java","lineNumber":93,"sourceCode":"                b = tmp;\n            }\n\n            // b >= a; subtract a from b (result is even)\n            b = b - a;\n        }\n\n        // Restore common powers of two\n        return a << commonTwos;\n    }\n\n    /**\n     * Helper to return absolute value of x unless x == Long.MIN_VALUE, in which\n     * case we delegate to BigInteger and throw to indicate overflow.\n     */\n    private static long absOrThrowIfOverflow(long x) {\n        if (x == Long.MIN_VALUE) {\n            // |Long.MIN_VALUE| = 2^63 which does not fit into signed long\n            throw new ArithmeticException(\"Absolute value of Long.MIN_VALUE does not fit into signed long. Use gcdBig() for full-range support.\");\n        }\n        return (x < 0) ? -x : x;\n    }\n\n    /**\n     * Computes GCD for an array of {@code long} values. Returns 0 for empty/null arrays.\n     * If any intermediate gcd cannot be represented in signed long (rare), an ArithmeticException\n     * will be thrown.\n     */\n    public static long gcd(long... values) {\n\n        if (values == null || values.length == 0) {\n            return 0L;\n        }\n        long result = values[0];\n        for (int i = 1; i < values.length; i++) {\n            result = gcd(result, values[i]);\n            if (result == 1L) {","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java#L75-L111","documentation":"Thrown by BitwiseGCD (via the private absOrThrowIfOverflow helper) when an input equals Long.MIN_VALUE and the gcd operation requires its absolute value as a signed long. |Long.MIN_VALUE| = 2^63 cannot be represented in a signed 64-bit long (max 2^63-1), so the operation overflows. The fix is to use the gcdBig(BigInteger) overload which handles the full range.","triggerScenarios":"Calling `gcd(Long.MIN_VALUE, 0)` or `gcd(0, Long.MIN_VALUE)` (the trivial zero cases hit absOrThrowIfOverflow directly), or any gcd path that needs abs(MIN_VALUE) before delegating to BigInteger. Note: when BOTH inputs are non-zero and one is MIN_VALUE, gcd(long,long) delegates to gcdBig and uses longValueExact() instead.","commonSituations":"GCD of sentinel/underflow long values; processing extreme numeric data where a value wrapped to Long.MIN_VALUE via overflow; cryptographic or combinatorial code using full-range longs.","solutions":["Switch to the BigInteger API: use gcdBig(a, b) instead of gcd(a, b) when inputs may be Long.MIN_VALUE.","Filter or clamp Long.MIN_VALUE before calling the long overload if BigInteger is undesirable.","Use the convenience overload gcdBig(long, long) which accepts signed 64-bit inputs and returns BigInteger."],"exampleFix":"// before\nlong g = BitwiseGCD.gcd(Long.MIN_VALUE, 0L);  // throws ArithmeticException\n\n// after\nBigInteger g = BitwiseGCD.gcdBig(Long.MIN_VALUE, 0L);  // returns 2^63","handlingStrategy":"validation","validationCode":"public static long safeGcd(long a, long b) {\n    if (a == Long.MIN_VALUE || b == Long.MIN_VALUE) {\n        // use BigInteger path for full range\n        return BitwiseGCD.gcdBig(a, b).longValueExact();\n    }\n    return BitwiseGCD.gcd(a, b);\n}","typeGuard":"public static boolean needsBigIntegerGcd(long a, long b) {\n    return a == Long.MIN_VALUE || b == Long.MIN_VALUE;\n}","tryCatchPattern":"try {\n    return BitwiseGCD.gcd(a, b);\n} catch (ArithmeticException e) {\n    return BitwiseGCD.gcdBig(a, b).longValueExact();\n}","preventionTips":["Prefer gcdBig(BigInteger) when inputs may be Long.MIN_VALUE.","Filter extreme values before calling the long overload.","Document which inputs can reach MIN_VALUE in your data flow."],"tags":["bit-manipulation","gcd","overflow","arithmeticexception","long-min-value"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}