AlgoMaster Logo

Absolute Value Without Built-in

Last Updated: June 7, 2026

easy
4 min read

Understanding the Problem

Return how far an integer sits from zero, without any library helper.

The definition splits into two cases. When n is zero or positive, its absolute value is n. When n is negative, flip its sign so the result becomes positive. So the problem reduces to detecting the sign of n and negating only when needed.

There are two ways to do that. One compares against zero and negates with a minus sign. The other uses the bit pattern of a signed integer to negate without any branch. Both produce the same answer.

Key Constraints:

  • -2^31 <= n <= 2^31 - 1 → This is the full range of a 32-bit signed integer. The negative side reaches -2147483648 (-2^31), but the positive side stops at 2147483647 (2^31 - 1).
  • The asymmetry above creates one edge case worth calling out. The absolute value of -2147483648 is 2147483648, which does not fit in a 32-bit signed integer. Negating it overflows and wraps back to -2147483648 in both approaches below. The test inputs stay within the safely representable range (down to -2147483647), but in real code this INT_MIN case has to be handled with a wider type or an explicit guard.

Approach 1: Conditional Negation

Intuition

Translate the definition straight into code. If n is negative, return -n, which turns the value positive. Otherwise n is zero or positive, so return it unchanged.

Algorithm

  1. Check whether n is less than 0.
  2. If it is, return -n (the negation, which is positive).
  3. Otherwise return n as is.

Example Walkthrough

Take n = -5. Since -5 < 0, we return -(-5), which is 5.

Take n = 42. Since 42 is not less than 0, we return it unchanged, giving 42.

Code

This version carries a branch. The same result can come purely from the integer's bit pattern, with no comparison.

Approach 2: Bit Manipulation

Intuition

Two's complement representation lets us negate using bitwise operations instead of a comparison.

Start with the sign bit. For a 32-bit integer, an arithmetic right shift by 31 positions copies the sign bit into every position. A negative number produces -1, which is all 1 bits. Zero or a positive number produces 0, which is all 0 bits. Call this value mask.

In two's complement, -n equals (~n) + 1, where ~n flips every bit. XORing with all 1 bits (-1) flips every bit, and subtracting -1 adds 1. So (n ^ mask) - mask does two different things depending on the sign:

  • When n is negative, mask is -1. Then n ^ mask flips every bit of n, and subtracting mask adds 1. Together that is (~n) + 1, which is exactly -n. The result is positive.
  • When n is non-negative, mask is 0. Then n ^ 0 is n, and subtracting 0 leaves it alone. The result is n unchanged.

A single expression covers both cases with no branch.

Algorithm

  1. Compute mask = n >> 31, using an arithmetic right shift on the 32-bit value. This gives all 1 bits for negatives and all 0 bits otherwise.
  2. Return (n ^ mask) - mask.

Example Walkthrough

Trace n = -5.

-5
n

Step 1: Compute the mask. -5 >> 31 copies the sign bit across all 32 positions, giving all 1 bits, which is -1. So mask = -1.

Step 2: XOR n with mask. In 32-bit form, -5 is ...11111011. XORing with -1 (all 1 bits) flips every bit to ...00000100, which is 4. So n ^ mask = 4.

Step 3: Subtract mask. 4 - (-1) = 4 + 1 = 5.

The result is 5, the absolute value of -5.

For a non-negative input like n = 42, the mask is 0. Then 42 ^ 0 = 42 and 42 - 0 = 42, leaving the value unchanged.

Code