Click here to Skip to main content
15,887,214 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralMean People Suck Pin
etkid8410-Oct-23 4:25
etkid8410-Oct-23 4:25 
GeneralC++ integer arithmetic overflow Pin
BernardIE53176-Oct-23 4:37
BernardIE53176-Oct-23 4:37 
JokeRe: C++ integer arithmetic overflow Pin
Jeremy Falcon6-Oct-23 4:46
professionalJeremy Falcon6-Oct-23 4:46 
GeneralRe: C++ integer arithmetic overflow Pin
Mike Hankey6-Oct-23 4:53
mveMike Hankey6-Oct-23 4:53 
AnswerRe: C++ integer arithmetic overflow PinPopular
JudyL_MD6-Oct-23 4:54
JudyL_MD6-Oct-23 4:54 
GeneralRe: C++ integer arithmetic overflow - danger, danger Pin
jmaida6-Oct-23 16:15
jmaida6-Oct-23 16:15 
GeneralRe: C++ integer arithmetic overflow Pin
Member 133016798-Oct-23 21:42
Member 133016798-Oct-23 21:42 
GeneralRe: C++ integer arithmetic overflow Pin
Martijn Smitshoek9-Oct-23 9:19
Martijn Smitshoek9-Oct-23 9:19 
As mentioned above, specifically undefined behavior might turn out in such a way that the "signed"ness does not necessarily flip as you would expect. Having said that, I don't know of a CPU/compiler that doesn't simply let the integers wrap around because that is still the easiest to implement.

I was almost trying to propose some right-shift logic trick to check, but, the right-shift is, in fact, undefined for signed integers as well and may give invalid results.

In assembly it's plain simple, you typically check the carry or overflow flag for unsigned or signed integers, respectively.

Finally, a watertight solution would be to cut every integer in two halves using masks and shifts, then craft your own carry- and overflow logic. I'm too lazy to work it out in detail but I'm positive that all the information you need to detect overflow is in the high bits of the upper sum. Something like, upper half, or upper half + 1 bits must turn out equal, and if not, there is overflow.
GeneralRe: C++ integer arithmetic overflow Pin
jeron16-Oct-23 4:54
jeron16-Oct-23 4:54 
GeneralRe: C++ integer arithmetic overflow Pin
OriginalGriff6-Oct-23 4:55
mveOriginalGriff6-Oct-23 4:55 
GeneralRe: C++ integer arithmetic overflow Pin
PIEBALDconsult6-Oct-23 6:04
mvePIEBALDconsult6-Oct-23 6:04 
GeneralRe: C++ integer arithmetic overflow Pin
k50546-Oct-23 6:13
mvek50546-Oct-23 6:13 
GeneralRe: C++ integer arithmetic overflow Pin
jschell6-Oct-23 7:23
jschell6-Oct-23 7:23 
GeneralRe: C++ integer arithmetic overflow Pin
RedDk6-Oct-23 8:11
RedDk6-Oct-23 8:11 
GeneralRe: C++ integer arithmetic overflow Pin
0x01AA6-Oct-23 8:35
mve0x01AA6-Oct-23 8:35 
JokeRe: C++ integer arithmetic overflow Pin
Nelek6-Oct-23 8:55
protectorNelek6-Oct-23 8:55 
JokeRe: C++ integer arithmetic overflow Pin
0x01AA6-Oct-23 9:06
mve0x01AA6-Oct-23 9:06 
GeneralRe: C++ integer arithmetic overflow Pin
Nelek6-Oct-23 9:10
protectorNelek6-Oct-23 9:10 
GeneralRe: C++ integer arithmetic overflow Pin
0x01AA6-Oct-23 9:16
mve0x01AA6-Oct-23 9:16 
GeneralRe: C++ integer arithmetic overflow Pin
Member 133016799-Oct-23 0:46
Member 133016799-Oct-23 0:46 
GeneralRe: C++ integer arithmetic overflow Pin
JSilvers9-Oct-23 8:07
professionalJSilvers9-Oct-23 8:07 
GeneralRe: C++ integer arithmetic overflow Pin
Lurk9-Oct-23 7:07
Lurk9-Oct-23 7:07 
GeneralRe: C++ integer arithmetic overflow Pin
Martijn Smitshoek9-Oct-23 9:30
Martijn Smitshoek9-Oct-23 9:30 
GeneralRe: C++ integer arithmetic overflow Pin
David On Life9-Oct-23 12:59
David On Life9-Oct-23 12:59 
GeneralRe: C++ integer arithmetic overflow Pin
BernardIE531710-Oct-23 4:31
BernardIE531710-Oct-23 4:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.