answersLogoWhite

0


Best Answer

When adding two signed integrals (x and y) of the same type, the result cannot overflow when they have opposing signs. The result can only be x or y or somewhere in between, and all values in the range x to y are valid regardless of which is the larger. But when they have the same sign, there is a potential for overflow. Given that x + y = z has well-defined behaviour, we can perform the following pseudocode operations to test for an overflow in z:

if x=0 or y=0 then overflow:=false

else if x<0 and y>0 then overflow:=false

else if x>0 and y<0 then overflow:=false

else if x<0 and z>0 then overflow:=true

else if x>0 and z<0 then overflow:=true

else overflow:=false

The following example shows how we can implement this in C:

// Returns true if x + y = z overflows

bool int_sum_overflow (int x, int y, int z) {

if (!x !y (x<0 && y>0) (x>0 && y<0)) return false; return (x<0 && z>0) (x>0 && z<0);

}

When dealing with unsigned integrals, we must test the operation before we perform the operation. This is because overflowing an unsigned integral has undefined behaviour in much the same way that division by zero has undefined behaviour. If we perform the operation first and an overflow occurs, then it's already too late; undefined behaviour has unpredictable consequences. If we're lucky, our program will simply crash and no harm will be done. If not, we have no way of knowing what has happened. Even if our specific implementation provides well-defined behaviour, it cannot be guaranteed across all architectures and cannot be regarded as being portable. Where undefined behaviour is concerned, prevention is always better than cure.

Fortunately, x + y = z is relatively simple to test without the need to evaluate z first. Given that z cannot exceed the maximum unsigned integral without overflowing, it follows that if max - x were less than y then the result will overflow. This can be stated as follows:

if (max-x)<y then overflow:=true

else overflow:=false

Given that max is a well-defined constant, this is trivial to implement. The following example shows how we might implement this in C:

#include<limits.h> // for UINT_MAX

// Returns true if the sum of x and y would overflow:

bool uint_sum_overflow (unsigned x, unsigned y) {

return (UINT_MAX - x) < y;

}

The above examples deal with addition only but we can perform similar tests for subtraction and multiplication operations. Aside from divide by zero, integral division operations present no major problems, although most divisions will result in a truncation of any fractional component.

If we want to improve efficiency then we really have to lift the bonnet and use assembly programming. In the case of unsigned integral addition, we need to test the carry bit in the most significant bit-adder. For signed integrals we need to test both carry out and the carry in bits of the most-significant bit-adder.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you handle a binary overflow?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the condition for the overflow in the linked lists?

In linked list if there is no any element inside it than we can say linked list is underflow.


What is meant by overflow in a digital circuit?

A overflow is a condition in which a calculation produces a unit of data too large to be stored in the location alloted to it. An overlow cannot happen when two numbers of opposite sign are added. An overflow may occur in an addition of binary numbers if the augend and addend are both positive or negative.


Write a c program to find the binary addition of two 8 bit numbers?

#include &lt;stdio.h&gt; #include &lt;iomanip&gt; int main() { int binary1[8] = {0,1,1,1,1,0,1,1}; //8 element array int binary2[8] = {1,0,1,0,1,1,1,1}; //8 element array int binarySum[9]; int overflow [9]; int i; for (i=0; i&lt;9; i++) overflow[i] = 0; for (i=9; i &gt;= 0; i--) { binarySum[i] = binary1[i] + binary2[i] + overflow[i]; if ( binarySum[i] &gt; 1) { overflow[i-1] = 1; binarySum[i] %= 2; } } printf("Binary Sum is: "); for (i = 0; i&lt;9; i++) printf("%i", binarySum[i]); printf("\n"); printf("\n"); printf("Carry Bit is: "); for (i = 8 ; i&gt;=0 ; i--) printf("%i", overflow[i]); printf("\n"); return (0); }


Was binary stands for binary digits?

No, binary is a number system.A binary digit is called a bit.


Does a bar sink have a overflow drain?

Most of the one's I have run across do not have an overflow. Mainly used for disposal, dumping out drinks, etc, not for washing so overflow not really an issue. Also, most are stainless which do not lend themselves to an overflow drain.

Related questions

What do you mean by arithmetic overflow?

arithmetic overflow is a situation that occurs when a calculation or operation yields a result that is too large for the system storage or register to handle. Overflow can also refer to the amount the result exceeds the memory designated for storage. ( basically too much, That's why its called overflow)


What is the condition for the overflow in the linked lists?

In linked list if there is no any element inside it than we can say linked list is underflow.


Why does the overflow reservoir overflow?

because the entire point of the overflow resovoir is to overflow, and release uneeded water


Which two Latin words mean overflow?

There are a number of (single) words available, inrigo as in to overflow or irrigate, abundo as in to be abundant and overflow, superfundo as in to flood or overflow, restagno as in to be swamped or overflow, exundo as in to flow out or overflow, adfluentia as in to overflow with abundance


Can a microprocessor handle an analog signal?

No. All analogue information must be converted to digital information (binary encoded) before it can be processed.


What is a mizar?

the archaic word Mizar means cloak.in astronomy, it refers to a star in the handle of the Big Dipper.its located near Alcor in the the handle of the Big Dipper.And are actually a complex system. Alcor is a binary and Mizar is a quadruple system of two binary stars.


When was Clancy of the Overflow created?

The Overflow of Clancy was created in 1892.


What is meant by overflow in a digital circuit?

A overflow is a condition in which a calculation produces a unit of data too large to be stored in the location alloted to it. An overlow cannot happen when two numbers of opposite sign are added. An overflow may occur in an addition of binary numbers if the augend and addend are both positive or negative.


Write a c program to find the binary addition of two 8 bit numbers?

#include &lt;stdio.h&gt; #include &lt;iomanip&gt; int main() { int binary1[8] = {0,1,1,1,1,0,1,1}; //8 element array int binary2[8] = {1,0,1,0,1,1,1,1}; //8 element array int binarySum[9]; int overflow [9]; int i; for (i=0; i&lt;9; i++) overflow[i] = 0; for (i=9; i &gt;= 0; i--) { binarySum[i] = binary1[i] + binary2[i] + overflow[i]; if ( binarySum[i] &gt; 1) { overflow[i-1] = 1; binarySum[i] %= 2; } } printf("Binary Sum is: "); for (i = 0; i&lt;9; i++) printf("%i", binarySum[i]); printf("\n"); printf("\n"); printf("Carry Bit is: "); for (i = 8 ; i&gt;=0 ; i--) printf("%i", overflow[i]); printf("\n"); return (0); }


How do you clear a stack overflow?

How do you clear a stack overflow


What are the different types of collision resolution techniques?

Open addressing (closed hashing), The methods used include: Overflow block &amp; Closed addressing (open hashing) The methods used include: Linked list, Binary tree..


What benefit does the binary digit have over decimal in ters of the internal operation of computer?

Binary is easier for the computer to understand. It is also easier to handle and is not costly.