que-es-el-desbordamiento-en-binario

What is overflow in the binary system

  • 5 min

Until now we have considered that in binary representation you have as many positions as you want* (like when you write on paper, you can always add more digits).

Now we will consider the condition that you only have a fixed number of digits. This introduces a new problem: the overflow problem, which is what happens when you exhaust the available capacity.

Overflow occurs when the number we are trying to represent is greater than the maximum value that can be stored in the given container. In this case, the variable overflows and returns to 0.

The overflow problem is not unique to the binary system. It also happens to you, for example, if you have a combination lock with only 4 digits. When you pass 9999, you will go back to 0000.

curso-binario-desbordamiento

In binary, you will encounter the overflow problem constantly. Because, in general, your binary number will be stored “somewhere” in the computer’s memory (that memory has a size, and if you exceed it… 💥BOOM, overflow).

On the other hand, overflow is not catastrophic “per se”. That is, your computer won’t explode, nor will anything break. Simply the number you had stored will quietly and peacefully return to zero.

Of course, if you were using that number to count something… well frankly, it’s probably not good (if it was your WoW player’s level, you’ll go back to the beginning. If it’s an airplane, maybe it’s worse and it crashes to the ground).

But overflow is not bad. In fact, it is inevitable and even necessary in many processes. You just have to understand it, take it into account, and learn to control it.

Overflow Example

Let’s imagine we want to store the number 300 in decimal, in a binary number. But, we only have 8 bits.

In binary, the number 300 in decimal is 1 0010 1100 in binary. We would need 9 digits to store it. But since we only have 8 digits, the maximum we can store is 255 in decimal.

Therefore, if we count up from 0 to 300, when we reach 255 the counter will reset to 0.

Let’s see it step by step:

StepBinaryDecimalComment
00000 00000We start
10000 00011
20000 00102
I keep counting up
2551111 1111255We reach the max
2560000 00001We overflow
I keep counting up
3000010 110044

The stored result will be 0010 1100 in binary, which is 44 in decimal. This is the same number as if we had converted to binary and removed the bits that don’t fit (1 0010 1100 in this example, the leftmost 1).

If the number was signed (i.e., it has a sign) the problem is even worse. Continuing with the same example, suppose we only have 8 bits. But now, being a signed variable, the limits are -128 to 127.

If I want to store the number 300, it’s not just that it doesn’t fit by a little… it doesn’t even fit in two times 127!

Let’s see it step by step as well:

StepBinaryDecimalComment
00000 00000We start
10000 00011
20000 00102
I keep counting up
1270111 1111127Maximum positive
1281000 0000-128We switch to negatives
1291000 0001-127
I keep counting up
2551111 1111-1Maximum negative
2560000 00000We overflow
I keep counting up
3000010 110044

Just like in the unsigned case, we ended up with a positive number 44. But, along the way, we went through all the negatives from -128 to 0 (which, if not controlled, can be a real problem).

Overflow Management

We said that the most important thing is to understand overflow and know how to manage it. Overflow is influenced by:

  • The number of bits we are managing (8, 16, 32, 64…)
  • Whether our variable is signed or unsigned

In both cases, when we add one to our number, the binary number will increment. When it reaches the limit of its capacity, it will reset to zero.

As we have seen, it is even more complicated if the stored number represents a signed number. Because, the available range will be much smaller (half minus one), and also before overflowing, it will go through the entire range of negative numbers.

To avoid overflow when storing binary numbers in fixed-length containers, we must always keep in mind the range of values expected to be handled and choose the appropriate data size.

In programming environments, it is common to use data types with specific sizes (like uint8, int16, etc.) that guarantee a valid range of values and help us prevent unwanted overflows.

Furthermore, when performing arithmetic operations, it is important to check data limits to avoid a calculation resulting in a value outside the allowed range.