Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6807151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:47:49+00:00 2026-05-26T19:47:49+00:00

I would like to compute the absolute difference of two integers. Naively, this is

  • 0

I would like to compute the absolute difference of two integers. Naively, this is just abs(a - b). However, this has a couple of problems, depending on whether the integers are signed or unsigned:

  • For unsigned integers, a - b will be a large positive number if b is larger than a, and the absolute value operation will not fix that.

  • For signed integers, a - b may be outside of the range of values that can be represented as a signed integer, thus leading to undefined behavior. (Obviously, this means that the result will need to be an unsigned integer.)

How can these problems be addressed in an efficient way?

I would like an algorithm (or pair of algorithms) that works for both signed and unsigned integers, and does not rely on casting the values to a larger integer size.

(Also, to clarify: my question is not how to write this in code, but what exactly I should be writing in order to guarantee overflow-safety. Computing abs(a - b) as a signed value and then casting to an unsigned value doesn’t work, because signed integer overflow is typically an undefined operation, at least in C.)

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:47:50+00:00Added an answer on May 26, 2026 at 7:47 pm

    (I’ve been working this out on my own after asking the question — I thought it would be harder, and I’d still welcome other answers if there are better ones!)

    The solution for unsigned integers is relatively straightforward (as described in Jack Toole’s answer), and works by moving the (implied) conditional outside the subtraction so that we are always subtracting the smaller number from the larger one, and are not comparing a potentially-wrapped value to zero:

    if (a > b)
      return a - b;
    else
      return b - a;
    

    This just leaves the question of signed integers. Consider the following variation:

    if (a > b)
      return (unsigned) a - (unsigned) b;
    else
      return (unsigned) b - (unsigned) a;
    

    We can easily prove that this works by using modulo arithmetic. We know that a and (unsigned) a are congruent modulo UINT_MAX. Further, the unsigned-integer subtraction operation is congruent to actual subtraction modulo UINT_MAX, so combining these facts, we know that (unsigned) a - (unsigned) b is congruent to the actual value of a - b modulo UINT_MAX. Finally, because we know that the actual value of a - b must be between 0 and UINT_MAX-1, we know that this is an exact equality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two collections a and b . I would like to compute the
I would like to compute time that spans between two particular moments: - start
I have a couple of text inputs and I would like to compute the
I would like to compute sum of subtotals dynamically but i always get this
I have data that I would like to compute some statistics with. The data
For given floating point numbers x and a , I would like to compute
I would like to compute similarity between licenses' txt files so I could then
I would like to compute both the sine and co-sine of a value together
I have a List, and I would like to compute a double[360] containing the
I would like to compute the maximum of translated images along the direction of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.