Integer overflow: Difference between revisions

Content deleted Content added
Adding paragraph to better explain the background of the data type Integer and Overflows, and how the most common implementation can have operations resulting in Integer Overflows.
Tags: Mobile edit Mobile web edit
Better link for int32 min value
Tags: Mobile edit Mobile web edit
Line 6:
In [[computer programming]], an '''integer overflow''' occurs when an [[arithmetic]] operation on integers attempts to create a numeric value that is outside of the range that can be represented with a given number of digits – either higher than the maximum or lower than the minimum representable value.
 
Integer overflow specifies an overflow of the [[Data type|data type]] [[Integer (computer science)|integer]]. An overflow (of any type) occurs when a [[Computer program|computer program]] or system tries to store more data in a fixed-size ___location than it can handle, resulting in [[Data loss|data loss]] or [[Data corruption|corruption]].<ref>{{cite web|url=https://www.lenovo.com/us/en/glossary/overflow-error}}</ref> The most common implementation of integers in modern computers are [[Two's complement|two's complement]].<ref>E.g. "Signed integers are two's complement binary values that can be used to represent both positive and negative integer values", Section 4.2.1 in ''Intel 64 and IA-32 Architectures Software Developer's Manual'', Volume 1: Basic Architecture, November 2006</ref> In two's complement the [[Bit numbering#Most_significant_bit|most significant bit]] represents the [[Sign bit|sign]] (positive or negative), and the remaining [[Bit numbering#Signed_integer_example|least significant bits]] represent the number. Unfortunately, for most [[Computer architecture|architectures]] the [[Arithmetic logic unit|ALU]] doesn't know the [[Binary number|binary representation]] is [[Signedness|signed]]. [[Two's_complement#Arithmetic_operations|Arithmetic operations]] can result in a value of bits exceeding the fixed-size of bits representing the number, this causes the sign bit to be changed, an integer overflow. The most infamously examples are: [[2,147,483,647#In_computing|2,147,483,647]] + 1 = -2,147,483,648 and [[Integer32-bit (computer science)computing#Long_integerRange_for_storing_integers|-2,147,483,648]] - 1 = 2,147,483,647.
 
On some processors like [[graphics processing unit]]s (GPUs) and [[digital signal processor]]s (DSPs) which support [[saturation arithmetic]], overflowed results would be ''clamped'', i.e. set to the minimum value in the representable range if the result is below the minimum and set to the maximum value in the representable range if the result is above the maximum, rather than wrapped around.