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 8223109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:33:32+00:00 2026-06-07T14:33:32+00:00

I was reading about techniques to detect overflow in C . one of the

  • 0

I was reading about techniques to detect overflow in C . one of the examples to show incorrect solution to detect overflow in addition was this one :

/* Determine whether arguments can be added without overflow */
int tadd_ok(int x, int y) {
    int sum = x+y;
    return (sum-x == y) && (sum-y == x);
}

and it said it doesn’t work because :

two’s-complement addition forms an abelian group, and so the
expression (x+y)-x will evaluate to y regardless of whether or not the
addition overflows, and that (x+y)-y will always evaluate to x

What does it exactly mean ? Does it mean that C compiler replace sum with x+y ?
To figure out what is it saying I even traced assembly code of the program, but there was no sign of replacement .

Update : The essence of my question is, does GCC evaluates an expression without calculating it ?
This is NOT a question about two’s complement.
You can see a sample output in here .

  • 1 1 Answer
  • 0 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-06-07T14:33:34+00:00Added an answer on June 7, 2026 at 2:33 pm

    If you take a trivial example of 4 (0b0100) + 5 (0b0101) you can see that the unsigned sum should be 9 (1001) which is actually -7 in two’s complement. If you then take that sum (0b1001) and subtract 4 from it using two’s complement arithmetic:

        0b1001 - 0b0100 = 0b1001 + 2s_complement(0b0100) = 0b1001 + 0b1100 = 0b1_0101 
    

    you end up with 0101 which is 5 (you drop the overflowing most significant 1 during a 2’s complement operation). Subtracting 5 from the sum equals 4:

        0b1001 - 0b0101 = 0b1001 + 2s_complement(0b0101) = 0b1001 + 0b1011 = 0b1_0100
    

    This satisfies the c code you provided but still resulted in an overflow.

    From wikipedia’s article on two’s complement:

    Two's complement    Decimal
    0111                 7
    0110                 6
    0101                 5
    0100                 4
    0011                 3
    0010                 2
    0001                 1
    0000                 0
    1111                −1
    1110                −2
    1101                −3
    1100                −4
    1011                −5
    1010                −6
    1001                −7
    1000                −8
    

    Update:
    To demonstrate your INT_MAX example using my trivial 4 bit integer system with INT_MAX = 7 we can see the same result as your c code.

        7 + 7 (0b0111 + 0b0111) = 0b1110 (-2 in two's complement)
    

    Just like my example above, subtracting, sum - 7 will equal 7.

        0b1110 - 0b0111 = 0b1110 + 2s_complement(0b0111) = 0b1110 + 0b1001 = 0b1_0111
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading through this excellent article about safe construction techniques by Brain Goetz, I got
Reading about Kohana templates and saw something I've never seen before: $this->template->title = __('Welcome
Reading about Django, I saw this: http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#ref-contrib-admin - the fancy simple to use admin
Yesterday I was reading about debugging techniques and found Valgrind to be really interesting.
I was reading about memory management techniques and was wondering: Why does every process
I'm reading about this ajax response that describes ways to prevent Javascript based exploits.
I was reading and hearing some stuff about cloud computing and map-reduce techniques lately.
My question vaguely relates to this one . However, it doesn't address the techniques
I am reading about automated bug finding techniques and in ( Valgrind ) paper
I have been reading about the continuation passing style programming technique (C# 3.0 implementation).

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.