About how to determine whether the unsigned integer addition overflows

Source: Internet
Author: User

Recently, many unsigned integer binary operations have been used in the code, so I am afraid that the addition operation will overflow.

Therefore, it is necessary to add an overflow detection.

About overflow, http://www.phrack.com/issues.html? Issue = 60 & id = 10. This article is very clear.

It is easier to detect the overflow of unsigned integers:

First, in the unsigned representation, A + 2 ^ n =;

If two unsigned integers A and B are not exceeded: A <2 ^ N, B <2 ^ N, and a + B> 2 ^ N,

Then sum = a + B-2 ^ N, that is, Sum-A = B-2 ^ n <0 => sum <A, similarly, sum <B.

This method can be used to determine whether the sum of two unsigned integers overflows. How can we determine the sum of multiple unsigned integers? The stupid method I think of now is to sum up and judge ......

The code above is ugly ......

 1 //! @brief check whether the usinged add will result in overflow 2 template<typename input_iterator> 3 int is_unsinged_add_overflow(input_iterator begin, 4                              input_iterator last) 5 { 6   typedef typename Iterator_Traits<input_iterator>::value_type value_type; 7   if(!(boost::is_same<value_type,size_t>::value) 8       && !(boost::is_same<value_type,unsigned int>::value) 9       && !(boost::is_same<value_type,unsigned long>::value)10       && !(boost::is_same<value_type,unsigned short>::value)){11     return -1;12   }13   std::stack<value_type> temp_sum;14   input_iterator it = begin;15   while(it != last){16     value_type a = *it++;17     if(it == last) {18       temp_sum.push(a);19       break;20     }21     value_type b = *it++;22     value_type sum = a + b;23     if(sum < a || sum < b) return 0; // overflow24     temp_sum.push(sum);25   }26   while(!temp_sum.empty()){27     if(temp_sum.size() == 1) return 1;// no overflow28     std::stack<value_type> forwad_sum;29     while(!temp_sum.empty()){30       value_type a = temp_sum.top();31       temp_sum.pop();32       if(temp_sum.empty()) forwad_sum.push(a);33       else{34         value_type b = temp_sum.top();35         temp_sum.pop();36         value_type sum = a + b;37         if(sum < a || sum < b) return 0; // overflow38         forwad_sum.push(sum);39       }40     }41    swap(temp_sum,forwad_sum);42   }43 }

Troublesome traits, making up your own special features ......

 1 template <typename T> 2 struct Iterator_Traits 3 { 4   typedef typename T::value_type value_type; 5 }; 6  7 template <typename type> 8 struct Iterator_Traits<type*> 9 {10   typedef type value_type;11 };

The code is compiled in Ubuntu 11.04 64bit, G ++-4.5.2, and is poorly written ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.