Why Math.abs() Returns a NEGATIVE Number
π― The Questionβ
"In Java, C++, and C#, what does
Math.abs(Integer.MIN_VALUE)return? Why does a mathematical absolute value function return a negative number (-2,147,483,648), and how can this crash hash-based sharding algorithms?"
β‘ 30-Second Elevator Pitchβ
Modern computer architectures represent signed integers using Two's Complement notation.
In a 32-bit signed integer (int):
- The range is strictly Asymmetric:
- Notice that the negative spectrum has one more number than the positive spectrum because
0occupies one of the positive binary states!
When you compute the absolute value of :
- The true mathematical answer is .
- But the maximum positive value a 32-bit integer can store is only !
- Negating
-2,147,483,648overflows the 32-bit boundary, producing-2,147,483,648.
π§ Under-the-Hood: Two's Complement Inversion Mathβ
The hardware simply inverts the bits and adds 1. For Integer.MIN_VALUE, this arithmetic operation results in the exact same binary pattern with the sign bit set to 1!
π¬ How This Crashes Hash Sharding in Productionβ
Developers frequently use Math.abs() to map hash codes to positive array or database shard indices:
// β PRODUCTION BUG: Crashes on Integer.MIN_VALUE!
int shardId = Math.abs(key.hashCode()) % numShards;
// If key.hashCode() == Integer.MIN_VALUE:
// Math.abs(...) returns -2147483648!
// -2147483648 % 10 = -8 -> Array/Shard Index Out of Bounds! π₯
Production Fixesβ
- Bitwise Masking (Eliminates Sign Bit):
int shardId = (key.hashCode() & 0x7FFFFFFF) % numShards; // Guaranteed positive! - Java 15+ Math.absExact():
Throws an explicit
ArithmeticExceptionon overflow rather than silently propagating corrupt negative values. - Promote to 64-bit Long:
long safeVal = Math.abs((long) val);
π Comparison Matrix: Integer Bounds and Two's Complementβ
| Metric | Minimum Value | Maximum Value | Total Distinct Values |
|---|---|---|---|
8-Bit Signed (byte) | 256 | ||
16-Bit Signed (short) | 65,536 | ||
32-Bit Signed (int) | |||
64-Bit Signed (long) |
π‘ What Interviewers Ask Next (Follow-Up Traps)β
-
"Why does Java's
%operator return negative numbers?"- Answer: In Java and C++,
%is the remainder operator, not the mathematical modulo operator. It preserves the sign of the dividend:-7 % 3 = -1. To get a true mathematical positive modulo, useMath.floorMod(a, b):Math.floorMod(-7, 3); // Returns 2!
- Answer: In Java and C++,
-
"What happens if you execute
Integer.MIN_VALUE / -1?"- Answer: It overflows and returns
Integer.MIN_VALUE(-2,147,483,648)! In C++, signed integer overflow is undefined behavior (UB); in Java, it wraps silently without an exception.
- Answer: It overflows and returns
Interview Answer: Math.abs(Integer.MIN_VALUE) returns a negative number because Two's Complement integer representations are asymmetric (ranging from to ). The positive counterpart cannot fit in a 32-bit signed integer, overflowing back to . Never use Math.abs() for shard indexing; use bitmasking (hash & 0x7FFFFFFF).