kids encyclopedia robot

Integer overflow facts for kids

Kids Encyclopedia Facts
Odometer rollover
When an odometer reaches its highest number (like 999,999), it rolls over to 0. This is similar to how an integer overflow works in computers.

In computer programming, an integer overflow happens when a computer tries to store a number that is too big or too small for the space it has. Imagine you have a small box that can only hold numbers up to 100. If you try to put 101 into it, that's an overflow!

Often, when an overflow happens, the number "wraps around." This means it goes back to the beginning of its range. For example, if the maximum number is 255 and you add 1, it might become 0. This is like an odometer rolling over from 999,999 to 0.

Sometimes, instead of wrapping, the number gets "clamped." This means if it's too big, it just becomes the maximum possible value. If it's too small, it becomes the minimum possible value.

When an overflow isn't expected, it can cause programs to act strangely. This can even lead to security problems. However, for some things like timers, wrapping around can actually be useful!

How It Happens

Computers store numbers in special places called "registers." The size of these registers limits how big a number can be. This size is measured in "bits." More bits mean bigger numbers can be stored.

Here are some common sizes for numbers in computers:

  • 4-bit: Can hold numbers up to 15.
  • 8-bit: Can hold numbers up to 255.
  • 16-bit: Can hold numbers up to 65,535.
  • 32-bit: Can hold numbers up to 4,294,967,295. This was common for personal computers around 2005.
  • 64-bit: Can hold numbers up to 18,446,744,073,709,551,615. This is common for computer central processing units (CPUs) today (as of 2024).
  • 128-bit: Can hold even larger numbers!

When you do math with numbers that are too big for their register, they "wrap around." This means only the last few digits of the result are kept.

For example, if you add 255 + 2 using an 8-bit number, the answer should be 257. But since 255 is the max, it wraps around to 1. Similarly, if you subtract 1 from 0, it might wrap around to 255.

This wrapping can be dangerous. If a program uses an overflowed number to decide how much memory to use, it might set aside too little space. This can lead to other problems, like a buffer overflow, which can be a security risk.

Sometimes, a number that should always be positive can become negative due to an overflow. For example, adding 127 + 1 with an 8-bit signed number (which can be positive or negative) can result in -128. This can confuse the program.

Checking for Overflow

Most computers have special "flags" that tell them if an overflow has happened.

  • The carry flag is set if an addition or subtraction goes beyond the number of bits available, like when you "carry over" a number in regular addition. This is for numbers that are always positive.
  • The overflow flag is set if a math operation on numbers that can be positive or negative gives a result with the wrong sign. For example, adding two positive numbers and getting a negative result.

How to Handle Overflow

It's important for programmers to deal with integer overflows. Here are some ways:

Finding Overflows

Special tools can help find overflows while a program is running. For example, some programming languages like Java have methods that will cause an error if an overflow happens.

Preventing Overflows

The best way to avoid overflows is to use number types that are big enough for any value they might hold. Programmers can also carefully plan their math operations. They can check numbers before doing calculations to make sure the result won't be too big.

Dealing with Overflows

If an overflow is expected, programmers can add checks to their code. If an overflow is about to happen, the program can do something else. For example, it might stop, tell the user there's a problem, and ask for different input.

Computers can also do math with numbers larger than their register size. This is called "multiple-precision arithmetic." They do this by breaking big numbers into smaller parts and adding them piece by piece, using the carry flag to keep track.

Programming Language Support

Different programming languages handle overflows in various ways:

Integer overflow handling in various programming languages
Language Unsigned (positive only) Signed (positive and negative)
Ada wraps around causes an error
C, C++ wraps around can cause unexpected behavior
C# wraps around or causes an error (programmer's choice) wraps around or causes an error (programmer's choice)
Java wraps around wraps around
JavaScript all numbers are very large floating-point numbers all numbers are very large floating-point numbers
Python 2 N/A automatically uses bigger numbers if needed
Swift causes an error unless special math is used causes an error unless special math is used

Some newer languages, like Rust, give programmers more control. They have built-in ways to check if an overflow happened and decide what to do.

Saturated Arithmetic

In areas like computer graphics, numbers often represent things like colors or brightness. For example, 0 might be black and 1 might be white. If you try to make something "brighter than white," saturated arithmetic just makes it white (the maximum value) instead of wrapping around. This makes sense for images.

Real-World Examples

Integer overflow bugs can be hard to find because they often only show up with very large numbers.

A famous example happened with the Therac-25 radiation therapy machines between 1985 and 1987. A software bug, partly due to an integer overflow, caused serious problems.

In 1996, the Ariane 5 rocket crashed on its first flight. This was caused by an integer overflow in its steering software. The software was designed for a smaller rocket and couldn't handle the higher speeds of the Ariane 5. When the overflow happened, it caused the rocket's steering to go wild.

On April 30, 2015, the U.S. Federal Aviation Administration told Boeing 787 operators to regularly reset their electrical systems. This was to avoid an integer overflow that could cause a loss of electrical power. Boeing later released a software update to fix this. The problem would happen after about 248 days of continuous operation, which pointed to a 32-bit number overflow.

Overflow bugs have also appeared in video games:

  • In Super Mario Bros. for the NES, if a player got 128 lives, the counter would roll over to 0. This meant the next time they died, it was game over!
  • In the arcade game Donkey Kong, you can't get past level 22. This is because the game's time/bonus calculation overflows, making it impossible to finish the level.
  • The famous "split-screen" level in Pac-Man is also caused by an overflow.
  • The "Far Lands" in Minecraft Java Edition (an old bug) were caused by an integer overflow when calculating world coordinates.
Error message due to an integer signedness bug in the stack setup code of MASM 1.00
This error message from an old computer program (MASM 1.00) was caused by an integer overflow. It prevented the program from running on computers with more than 512 KB of memory.

In August 2016, a casino machine printed a prize ticket for over $42 million due to an overflow bug. The casino refused to pay, saying it was a malfunction. The gaming commission agreed, as the machine clearly stated its maximum payout was $10,000.

See also

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