Integer overflow: Difference between revisions

Content deleted Content added
Line 29:
If the variable has a [[Signed number representations|signed integer]] type, a program may make the assumption that a variable always contains a positive value. An integer overflow can cause the value to wrap and become negative, which violates the program's assumption and may lead to unexpected behavior (for example, 8-bit integer addition of 127 + 1 results in -128, a two's complement of 128).
 
==Overflow flagsFlags==
Most computers distinguish betweenhave two kindsdedicated processor flags to check offor overflow conditions.
 
AThe [[carry (arithmetic)|''carry''flag]] occursis set when the result of an addition or subtraction, considering the operands and result as unsigned numbers, does not fit in the resultgiven number of bits. Therefore,This itindicates isan usefuloverflow towith a [[carry (arithmetic)|''carry''/''borrow'']] checkfrom the [[carrymost flagsignificant bit]]. afterAn addingimmediately following ''add with carry'' or subtracting''substract numberswith borrow'' operation would use the contents of this flag to modify a register or a memory ___location that arecontains interpretedthe ashigher unsignedpart values.of a multi-word value.
 
AnThe ''[[overflow'' properflag]] occursis set when the result of an operation on signed numbers does not have the sign that one would predict from the signs of the operands, (e.g. a negative result when adding two positive numbers). Therefore,This itindicates isthan usefulan to check the [[overflow flag]]has afteroccurred addingand orthe subtractingsigned numbers that areresult represented in [[two's complement]] form (i.e.would theynot arefit consideredin signedthe numbers)given number of bits.
 
==Methods to mitigate integer overflow problems==