kids encyclopedia robot

Integer overflow facts for kids

Kids Encyclopedia Facts
Odometer rollover
An old car's odometer shows what happens during an integer overflow. When the numbers hit the maximum (999999), adding one more kilometer makes it "wrap around" and reset to zero. The computer can't show the new, bigger number (1,000,000) because there's no space for the extra digit.

In computer programming, an integer overflow is a type of bug that happens when a computer tries to store a number that is too big for the space it has. Think of it like trying to pour a whole gallon of water into a small cup. The water will overflow because the cup can't hold it all.

When a computer does math, it has a fixed amount of space to hold the answer. If the answer is bigger than the maximum value that can be stored, or smaller than the minimum value, an overflow happens. This can cause the number to "wrap around" from the highest possible value to the lowest, leading to strange and unexpected results in programs and video games.

For example, in a system where the biggest positive number is 2,147,483,647, adding 1 to it might cause the number to suddenly become -2,147,483,648. This kind of error can cause serious problems if it's not handled correctly by the programmer.

Sometimes, this wrapping effect is useful, like in timers or clocks. But in most cases, an integer overflow is a bug that can affect a program's safety and cause it to crash or behave incorrectly.

What Causes an Integer Overflow?

Computers store numbers using a system of tiny switches called bits. Each bit can be either on (1) or off (0). The more bits a computer uses to store a number, the larger that number can be. This is similar to how an odometer in a car has a limited number of digits to show how far it has traveled.

For example, if a car's odometer has six digits, the highest number it can show is 999,999. If you drive one more kilometer, it doesn't show 1,000,000. Instead, it rolls over to 000,000. The computer does the same thing with bits. If it only has 8 bits to store a number, the highest value it can hold is 255. If you try to calculate 255 + 2, the result will wrap around to 1.

Signed and Unsigned Numbers

Programmers can use the available bits in two main ways:

  • Unsigned integers: These numbers can only be positive. All the bits are used to store the value of the number. For example, an 8-bit unsigned integer can store numbers from 0 to 255.
  • Signed integers: These numbers can be positive or negative. To do this, the computer uses one of the bits (the most significant bit) to represent the sign. An 8-bit signed integer can store numbers from -128 to 127.

Overflows with signed integers can be especially weird. If you have the number 127 and add 1, it might wrap around and become -128. This happens because the math operation changes the special "sign bit," tricking the computer into thinking the number is now negative.

What Happens When an Overflow Occurs?

When an integer overflow happens, the computer might do one of two things, depending on how it's programmed.

Wrapping

The most common result is that the number wraps around. This is what happens in the odometer example. The value goes from the maximum possible number to the minimum one.

  • Adding to the largest positive number can make it become the smallest negative number. (Example: 127 + 1 = -128)
  • Subtracting from the smallest number can make it wrap to a large positive number. (Example: 0 - 1 = 255 in an 8-bit unsigned system)

This wrapping behavior is the cause of many famous bugs and glitches in video games.

Saturation

Some systems, especially in graphics cards (GPUs), use a method called saturation arithmetic. Instead of wrapping around, the number is "clamped" at the highest or lowest possible value.

For example, if you are brightening a picture and a calculation makes a pixel's brightness value go above the maximum of 255, it will just stay at 255 (the brightest white). It won't wrap around to 0 (black). This is often more useful for things like graphics and sound processing.

Why Overflows Can Be a Big Problem

While sometimes harmless, integer overflows can have serious real-world consequences. They can create security holes in software or cause critical systems to fail.

  • Security Risks: If a program uses a number that has overflowed to decide how much memory to use for a task, it might use a much smaller amount than needed. This can lead to a buffer overflow, a serious security problem that hackers can use to run harmful code.
  • System Failures: Unhandled overflows have caused major disasters. The first flight of the Ariane 5 rocket in 1996 ended in an explosion just 40 seconds after launch. The cause was an integer overflow in the navigation software. The software tried to store a number that was too big for its 16-bit space, causing the rocket's guidance system to shut down.

On April 30, 2015, the U.S. Federal Aviation Administration warned that a bug in the Boeing 787 could cause the plane to lose all electrical power. The bug was an integer overflow that would happen if the plane's power systems were left on continuously for 248 days.

How Programmers Deal with Overflows

Programmers have several ways to prevent or handle integer overflow bugs.

  • Detection: Some programming languages can detect when an overflow is about to happen and stop the program or run special code to handle the error.
  • Avoidance: The simplest way to avoid an overflow is to use a data type that is large enough to hold any possible result. For example, using a 64-bit integer instead of a 32-bit one gives a much larger range of numbers.
  • Handling: If an overflow might happen, a programmer can add checks to the code. For example, before adding two large numbers, the code can check if their sum would be too big. If so, the program can show an error message instead of continuing with a wrong value.
  • Language Support: Modern languages like Rust and Swift have built-in features that make it easier for programmers to control what happens during an overflow. Older languages like C often leave it up to the programmer to be careful.


Typical Ranges for Integers
Bits Common Name Signed Range (Positive and Negative) Unsigned Range (Positive Only)
8-bit byte -128 to 127 0 to 255
16-bit short, word -32,768 to 32,767 0 to 65,535
32-bit int -2.1 billion to 2.1 billion 0 to 4.2 billion
64-bit long -9 quintillion to 9 quintillion 0 to 18 quintillion

Famous Examples in Video Games

Integer overflows are famous for causing glitches in classic video games.

  • Super Mario Bros.: In the original game for the NES, the number of lives was stored in a single signed byte (-128 to 127). If you used a trick to get more than 127 lives, the counter would overflow and roll over to a negative number, causing a game over the next time you lost a life.
  • Pac-Man: The famous "split-screen" bug on level 256 of the arcade game is caused by an integer overflow. The game tries to draw fruit at the bottom of the screen using the level number, but the calculation for level 256 overflows, causing the game to mess up the right side of the screen.
  • Donkey Kong: The arcade game crashes on level 22. The formula for calculating the bonus timer overflows when it gets to level 22, resulting in a timer so short that it's impossible to finish the level.
  • Minecraft: For many years, there was a strange place in Minecraft known as the "Far Lands." It was a glitched area that appeared very far from the player's starting point. This was caused by an integer overflow in the code that generated the world's landscape. The bug was fixed in later versions.

See also

de:Arithmetischer Überlauf

kids search engine
Integer overflow Facts for Kids. Kiddle Encyclopedia.