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

  • Home
  • SEARCH
  • 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 7571939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:47:43+00:00 2026-05-30T15:47:43+00:00

I know that when overflow occurs in C/C++, normal behavior is to wrap-around. For

  • 0

I know that when overflow occurs in C/C++, normal behavior is to wrap-around. For example, INT_MAX+1 is an overflow.

Is possible to modify this behavior, so binary addition takes place as normal addition and there is no wraparound at the end of addition operation ?

Some Code so this would make sense. Basically, this is one bit (full) added, it adds bit by bit in 32

int adder(int x, int y)
{
    int sum;
    for (int i = 0; i < 31; i++)
    {
        sum = x ^ y;
        int carry = x & y;
        x = sum;
        y = carry << 1;
    }

    return sum;
}

If I try to adder(INT_MAX, 1); it actually overflows, even though, I amn’t using + operator.

Thanks !

  • 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-05-30T15:47:44+00:00Added an answer on May 30, 2026 at 3:47 pm

    Overflow means that the result of an addition would exceed std::numeric_limits<int>::max() (back in C days, we used INT_MAX). Performing such an addition results in undefined behavior. The machine could crash and still comply with the C++ standard. Although you’re more likely to get INT_MIN as a result, there’s really no advantage to depending on any result at all.

    The solution is to perform subtraction instead of addition, to prevent overflow and take a special case:

    if ( number > std::numeric_limits< int >::max() - 1 ) { // ie number + 1 > max
        // fix things so "normal" math happens, in this case saturation.
    } else {
        ++ number;
    }
    

    Without knowing the desired result, I can’t be more specific about the it. The performance impact should be minimal, as a rarely-taken branch can usually be retired in parallel with subsequent instructions without delaying them.

    Edit: To simply do math without worrying about overflow or handling it yourself, use a bignum library such as GMP. It’s quite portable, and usually the best on any given platform. It has C and C++ interfaces. Do not write your own assembly. The result would be unportable, suboptimal, and the interface would be your responsibility!

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

Sidebar

Related Questions

I know that default cron's behavior is to send normal and error output to
Does anyone know if the problems that have been affecting Stack Overflow with regards
I know that the following is true int i = 17; //binary 10001 int
So I know that there is a very similar question to this all over
I know that there are plenty of similar questions on stack overflow. But the
I know that virtually anything is possible with CSS, but I'm a bit stumped
I know this topic has been discussed before on Stack Overflow. But there are
I know that normalization has been extensively discussed on Stack Overflow. I've read many
It's known that big local/global variables may cause to a stack overflow. I know
I know that I can do something like $int = (int)99; //(int) has a

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.