I am looking for a portable method of checking whether overflow has occurred when multiplying unsigned integers. The way I have always done it in the past is to turn my unsigned integers into uint64_t's, multiply, then create a uint64_t with a value of 2 to the 32nd power and if that is less than or equal to the product, I have overflowed. However, that assumes that unsigned ints are always 32 bits, which I cannot assume, so I want to ditch that bad habit. Plus that still leaves the problem of detecting overflow when multiplying two uint64_t values. I've been researching it online and a few people have suggested using compiler flags, which I am not crazy about because then I can't just post code online at a place like Daniweb and assume everyone will use the right flags.

Dusting the cobwebs off, I remember that internally, an overflow bit is set somewhere when the CPU executes a multiply operation and overflow occurs. That seems like the best way to do things since the CPU has already done it so there is nothing to code by me, correct? My questions are "Is this understanding correct?", "Can I access this bit using C++?", and "Is this indeed the best approach?"

Recommended Answers

All 6 Replies

OK, just thought of something that seems like it would work. If c = a * b, first test for a or b equalling 0 or 1. No overflow if either do. Now test that c > a and c > b? If not, overflow has occurred when using unsigned multiplication. If c is bigger than a and b, overflow has NOT occcurred. Seems like that is a good test and it will work for all unsigned types.

That said, even though (I think) I have solved my initial problem, I am still interested in knowing whether I can harness that Overflow flag in C++.

correct me if I am wrong and since I dont have my compiler in front of me I might be. If you have c = a * b, then can't you just do a check after the mulitplication for c / a == b? If c overflows it should break. The only case I can see in my head is if either a or b equals the maximum for the data type.

If c = a * b, first test for a or b equalling 0 or 1. No overflow if either do. Now test that c > a and c > b? If not, overflow has occurred when using unsigned multiplication. If c is bigger than a and b, overflow has NOT occcurred.

I don't think that's necessarily true. If I imagine, for test purposes, a counter that runs from zero to nine, and I calculate 7x7 (i.e. a=7, b=7) the correct answer would be 49, which in my simple counter will come out as 9. 9 is bigger than a and b, but there has been overflow. If your counter can hold a different range of values, the principle still holds.

Moschops, you're right! The test would work for adding, but not multiplication. Seems like Nathan's test would be a better one. I'll test it. As for Dave Sinkula's code, it's Dave, so I tend to simply assume it's tested and works, plus it makes sense. Thanks for the link mvmalderen.

It's sounding like the Overflow flag doesn't actually need to be checked, but it also seems to me like checking that would be the most fool-proof and easy thing to do in general, if not necessarily for this particular operation. Anyone know how to do it that way?

Anyone know how to do it that way?

Unless you feel like dropping down to inline assembly, the best (only) solution is to verify that the operation won't overflow before you perform it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.