r/ProgrammerHumor • • Aug 14 '20

Someone try this

Post image
53.6k Upvotes

433 comments sorted by

View all comments

3

u/Painfulyslowdeath Aug 14 '20

What logic/lines of code make it take an error like that and try to correct it by switching to the largest 32 bit integer?

1

u/slimscsi Aug 14 '20

Nothing tries to correct anything. This is standard twos compliment arithmetic. Except it totally wrong. Because a signed integer would go to -1, and a signed integer would be over 4 billion not 2.

1

u/techgineer13 Aug 14 '20

It's undefined behavior in unmanaged languages like C and C++, so most compilers just underflow it to "prevent" bugs.