Calculate binary overflow
Enter binary operands and choose the number of bits used for the calculation.
How binary overflow works
A computer stores fixed-width binary values. When an arithmetic operation produces a value outside the range supported by that width, overflow or underflow may occur. Signed and unsigned numbers use the same bits but interpret them differently.
Unsigned overflow
An unsigned number uses every bit to represent a non-negative value. For addition, overflow occurs when a carry leaves the most significant bit. For subtraction, an unsigned underflow occurs when A is smaller than B.
Unsigned range: 0 to (2n − 1)Signed overflow
In two’s-complement representation, the most significant bit is the sign bit. A zero sign bit represents a non-negative number, while a one sign bit represents a negative number.
Signed range: −2(n−1) to (2(n−1) − 1)Signed addition overflow rule
Adding two positive numbers should not produce a negative result, and adding two negative numbers should not produce a positive result. If the result sign differs from the operands’ common sign, signed overflow has occurred.
Addition overflow = (sign A = sign B) AND (sign result ≠ sign A)Signed subtraction overflow rule
Subtraction can overflow when the operands have different signs and the result sign differs from the sign of A.
Subtraction overflow = (sign A ≠ sign B) AND (sign result ≠ sign A)Example: signed overflow in 8-bit addition
Consider adding 127 and 1 using 8-bit signed two’s-complement arithmetic.
| Value | Binary | Signed decimal |
|---|---|---|
| Operand A | 01111111 |
127 |
| Operand B | 00000001 |
1 |
| Stored result | 10000000 |
-128 |
Frequently asked questions
What is binary overflow?
Binary overflow occurs when an arithmetic result is outside the range that can be represented using the selected number of bits.
What is the difference between signed and unsigned overflow?
Unsigned overflow concerns values from zero to the maximum unsigned value. Signed overflow concerns the positive and negative range represented by two’s complement. The same bit pattern can therefore produce different overflow results depending on its interpretation.
Does a carry-out always mean signed overflow?
No. Carry-out is used to detect unsigned addition overflow. Signed overflow is determined by comparing the signs of the operands and result, or equivalently by comparing the carry into and out of the sign bit.
How is subtraction calculated in binary?
A common hardware method converts subtraction into addition: invert every bit of B, add one, and then add the result to A. This is the two’s-complement method.
Why does the calculator limit the width to 32 bits?
The calculator uses PHP integer and bitwise operations. A maximum width of 32 bits keeps these operations within a safe and predictable range on supported 64-bit PHP environments.