Binary Addition. Is it overflow?
Binary values are in 2s Complement form.
If I am to add 110001 (-15) and 1开发者_高级运维01110 (-18), and the answer has to be stored in a 6-bit integer, is this an underflow/overflow.
This is overflow, your professor is correct. You are storing more bits that can be held in the alloted space (even though the number that the bits represent is negative.)
Underflow is when bits get zero'd out through shifting on big math. Very common in fixed point math. Divide a very small number by a very big number and you will quite often get a 0. That is underflow.
There is a a good example discussed to add two registers with contents -70 and -90 and store it in third register.
Let's assume
R1[8 bits] = -70
R2[8 bits] = -90
Result[9 bits] = -160, an extra bit for overflow.
This example is discussed on overflow_signed_detection page on ref 1 below. With the rules discussed on this page, the example can be scaled to any two signed decimal numbers.
EDIT: I just realized that -33 is too large for 6 bits, so the result is NOT -33 but +31, and thus it is definitely an overflow :)
Adding two numbers and getting the correct result if definitely NOT an overflow. An example of an overflow is adding two negative numbers and getting a positive number as a result (or vice versa).
For example, if you add the two positive numbers 0x7fffffff and 0x00000001, you get the negative number 0x80000000, which is definitely wrong and thus an overflow.
Maybe you are confusing overflow with carry?
精彩评论