Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to check if integer addition will overflow in C++

2 Answers

0 votes
#include <iostream>
#include <climits>

bool addingWillOverflow(int x, int y) {
    return ((x > 0) && (y > INT_MAX - x)) || ((x < 0) && (y < INT_MIN - x));
}

bool willAdditionOverflowUnsigned(unsigned int x, unsigned int y) {
    return (x > UINT_MAX - y);
}

// Some compilers, like GCC or Clang, provide built-in functions to detect overflow
bool willAdditionOverflowBuiltin(int a, int b) {
    int result;
    return __builtin_add_overflow(a, b, &result);
}

int main() {
    int x = 38839299, y = 2372783642;
    std::cout << (addingWillOverflow(x, y) ? "true" : "false") << std::endl;

    x = 2438839299; y = 3972783642;
    std::cout << (addingWillOverflow(x, y) ? "true" : "false") << std::endl;

    unsigned int x1 = 138839299, y1 = 2372783642;
    std::cout << (willAdditionOverflowUnsigned(x1, y1) ? "true" : "false") << std::endl;

    x1 = 4238839299; y1 = 2372783642;
    std::cout << (willAdditionOverflowUnsigned(x1, y1) ? "true" : "false") << std::endl;

    std::cout << (willAdditionOverflowBuiltin(x, y) ? "true" : "false") << std::endl;
}



/*
run:

false
true
false
true
true

*/

 



answered May 17, 2025 by avibootz
0 votes
#include <iostream>
#include <limits>

bool addingWillOverflow(int x, int y) {
    return ((x > 0) && (y > std::numeric_limits<int>::max() - x)) || ((x < 0) && (y < std::numeric_limits<int>::min() - x));
}

bool willAdditionOverflowUnsigned(unsigned int x, unsigned int y) {
    return (x > std::numeric_limits<unsigned int>::max() - y);
}

// Some compilers, like GCC or Clang, provide built-in functions to detect overflow
bool willAdditionOverflowBuiltin(int a, int b) {
    int result;
    return __builtin_add_overflow(a, b, &result);
}

int main() {
    int x = 38839299, y = 2372783642;
    std::cout << (addingWillOverflow(x, y) ? "true" : "false") << std::endl;

    x = 2438839299; y = 3972783642;
    std::cout << (addingWillOverflow(x, y) ? "true" : "false") << std::endl;

    unsigned int x1 = 138839299, y1 = 2372783642;
    std::cout << (willAdditionOverflowUnsigned(x1, y1) ? "true" : "false") << std::endl;

    x1 = 4238839299; y1 = 2372783642;
    std::cout << (willAdditionOverflowUnsigned(x1, y1) ? "true" : "false") << std::endl;

    std::cout << (willAdditionOverflowBuiltin(x, y) ? "true" : "false") << std::endl;
}



/*
run:

false
true
false
true
true

*/

 



answered May 17, 2025 by avibootz

Related questions

1 answer 130 views
1 answer 93 views
1 answer 114 views
1 answer 114 views
1 answer 104 views
1 answer 129 views
...