TheAlgorithms/Java · error · ArithmeticException
Absolute value of Long.MIN_VALUE does not fit into signed lo
Error message
Absolute value of Long.MIN_VALUE does not fit into signed long. Use gcdBig() for full-range support.
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java:93
b = tmp;
}
// b >= a; subtract a from b (result is even)
b = b - a;
}
// Restore common powers of two
return a << commonTwos;
}
/**
* Helper to return absolute value of x unless x == Long.MIN_VALUE, in which
* case we delegate to BigInteger and throw to indicate overflow.
*/
private static long absOrThrowIfOverflow(long x) {
if (x == Long.MIN_VALUE) {
// |Long.MIN_VALUE| = 2^63 which does not fit into signed long
throw new ArithmeticException("Absolute value of Long.MIN_VALUE does not fit into signed long. Use gcdBig() for full-range support.");
}
return (x < 0) ? -x : x;
}
/**
* Computes GCD for an array of {@code long} values. Returns 0 for empty/null arrays.
* If any intermediate gcd cannot be represented in signed long (rare), an ArithmeticException
* will be thrown.
*/
public static long gcd(long... values) {
if (values == null || values.length == 0) {
return 0L;
}
long result = values[0];
for (int i = 1; i < values.length; i++) {
result = gcd(result, values[i]);
if (result == 1L) {View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before long g = BitwiseGCD.gcd(Long.MIN_VALUE, 0L); // throws ArithmeticException // after BigInteger g = BitwiseGCD.gcdBig(Long.MIN_VALUE, 0L); // returns 2^63
Defensive patterns
Strategy: validation
Validate before calling
public static long safeGcd(long a, long b) {
if (a == Long.MIN_VALUE || b == Long.MIN_VALUE) {
// use BigInteger path for full range
return BitwiseGCD.gcdBig(a, b).longValueExact();
}
return BitwiseGCD.gcd(a, b);
} Type guard
public static boolean needsBigIntegerGcd(long a, long b) {
return a == Long.MIN_VALUE || b == Long.MIN_VALUE;
} Try / catch
try {
return BitwiseGCD.gcd(a, b);
} catch (ArithmeticException e) {
return BitwiseGCD.gcdBig(a, b).longValueExact();
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Arguments must not be null
- Invalid BCD digit: {}
- Value out of bounds for BCD representation: {}
- Shift amount cannot be negative: {}
- Bit positions must be between 0 and 31
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/389ac9459958f4d3.
Report an issue: GitHub.