All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is integer overflow?

Post cover: What is integer overflow?
Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
You have a counter that can only count to a certain number. For example, a pocket calculator that shows the maximum 999. If you try to add 1 to 999, it won't be able to show 1000 - the numbers will simply "overflow" and the calculator will show something completely different, for example 000.
In programming, such an effect is called integer overflow (integer overflow).
This happens when the number becomes larger than the maximum value that the selected variable type can store.
For example, if the variable int can store numbers up to 2,147,483,647, then adding 1 will make its value... negative!

Why 2,147,483,647 is the "ceiling" for a 32-bit number

In a computer, numbers are stored in binary form (that is, as a sequence of zeros and ones).
"32-bit" means that a number is allocated to store 32 cells, and each can have either 0 or 1.
But if the number signed (with sign), then one of these cells is reserved for sign:
  • 0 in the first bit — the number is positive,
  • 1 in the first bit — the number is negative.
That is, instead of 32 bits, only the number remains for the value 31 bits.
Maximum value
When all 31 bits (except the sign) are filled with units, we get the largest possible positive number:
1111111111111111111111111111111
In the decimal system, this is just the case 2,147,483,647.
In hexadecimal (hex) form, this is written as 0x7FFFFFFF:
  • 7 — is binary 0111 (ie sign 0 + three units),
  • F — is binary 1111 (four units),
  • and so on until the end.
Because of this, very strange bugs sometimes occur in programs, and there have even been serious accidents due to overflow in history. even memes occur due to an integer overflow error, but this is not accurate.

This post doesn't have any additions from the author yet.

What is the HEIC format and why just rename it .jpg — is a bad idea
15 Jun 18:17

What is the HEIC format and why just rename it .jpg — is a bad idea

meme code
meme code@memecode
Чому вибір CMS важливий під час розробки сайту?
29 Jun 12:34

Чому вибір CMS важливий під час розробки сайту?

meme code
meme code@memecode
Error 403 on the site: what it means and how to eliminate it
24 Jul 23:50

Error 403 on the site: what it means and how to eliminate it

meme code
meme code@memecode
What is vibe coding?
25 Jul 21:51

What is vibe coding?

meme code
meme code@memecode
What is a combinatorial explosion?
28 Jul 11:50

What is a combinatorial explosion?

meme code
meme code@memecode
What is a brain stack?
28 Jul 19:37

What is a brain stack?

meme code
meme code@memecode
What is HAR file (HTTP Archive)?
25 Aug 18:23

What is HAR file (HTTP Archive)?

meme code
meme code@memecode
What is Bubble Sort (algorithm explanation)?
16 Sep 18:42

What is Bubble Sort (algorithm explanation)?

meme code
meme code@memecode
What is exponential growth?
16 Sep 18:57

What is exponential growth?

meme code
meme code@memecode
What is factorial complexity?
16 Sep 19:03

What is factorial complexity?

meme code
meme code@memecode
What is NP-complexity?
16 Sep 19:31

What is NP-complexity?

meme code
meme code@memecode
Offset vs Cursor Pagination in Rails: What to Choose and Why
24 Sep 15:22

Offset vs Cursor Pagination in Rails: What to Choose and Why

meme code
meme code@memecode