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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:44:34+00:00 2026-05-30T07:44:34+00:00

While reading some question on a site I came across below question where a

  • 0

While reading some question on a site I came across below question where a c question needs to be debug

unsigned int a, b, c;
/* a and b are assume to have some values */
c = (a + b) / 2; // <- There is a bug in this st
What is the bug? and how you debug it?

Some of the answer saying it could cause overflow(c=(a+b)/2).but really didn’t get how it cause overflow?

  • 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-30T07:44:36+00:00Added an answer on May 30, 2026 at 7:44 am

    If a and/or b are very large then a + b could exceed the maximum size of an unsigned integer (see MAX_UINT in the limits.h file). This would cause an overflow and so the result would be wrong. For example if a and b are both equal to 0x80000000 the result would be 0 in 32-bit arithmetic, rather than expected result 0x80000000.

    To solve it you could use something like this instead:

    c = a/2 + b/2 + (a % 2 == 1 && b % 2 == 1);
    

    If you know that b is greater than a then you could use this slightly simpler version:

    c = a + (b - a) / 2;
    

    Read this article for information about how this bug appeared in binary search algorithms in may popular languages (though it talks about signed int rather than unsigned int):

    • Extra, Extra – Read All About It: Nearly All Binary Searches and Mergesorts are Broken
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this class while reading a C# book and have some questions.
While reading some Java source, I came across this line: ((Closeable) some_obj).close(); some_obj is
While reading some articles about writing web servers using Twisted , I came across
I have been reading about Scala for a while and even wrote some small
While reading a code I came across, the following definition and initialization of a
While reading SICP I came across logic programming chapter 4.4. Then I started looking
I'm having some troubles while reading from an exel in c#. I have this
First question ever on stackoverflow. Been reading for some time now, while trying to
While reading cocoa tutorials i've noticed that some of the tutorials use AppDelegate and
While reading through another question here, on creating a URL shortening service, it was

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.