retagged by
301 views
0 votes
0 votes
Can we have overflow in unsigned numbers ???

We know that in unsigned numbers with n bits we can represent numbers from 0 to 2^n - 1.EX: with 4 bits from o to 15.Now if I add 14 + 15 = 29 which we cant represent using 4 bits...can we say this as overflow ??

What I feel is In unsigned numbers whenever we get an end-around carry there will always be an overflow ..Please correct me if I am wrong....
retagged by

1 Answer

Best answer
1 votes
1 votes

Overflow occurs when the resulting value of an operation performed on valid representations of numbers is out of the range of valid values. That is, the resulting value is greater than max or less than min

If you are representing numbers using 4 bits as unsigned binary, the minimum value is 0 (i.e., 00002, while the maximum value is 15 (11112). Any operation that results in a number larger than 15 or smaller than 0 has overflowed.

For example, if you summed 14 + 15 (in UB, that's 1110 + 1111), the resulting value is 29, but the value 29 has no representation using 4 bits (since the maximum representable value is 15). Thus, there is overflow.

You might wonder, well, why not use 5 bits? Hardware has fixed number of bits. If it's 4 bits, then that's all you have to work with.

Detecting overflow in unsigned binary is straightforward. If adding the two values causes a N+1 bit result (this means that when you summed the MSb's plus any carry into the column of the MSBs, the resulting value had a carry to the next column), you have overflow.

https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/overflow.html

selected by

Related questions

0 votes
0 votes
1 answer
1
junior hacker asked Jan 15
151 views
a computer has 32-bit instructions and 12-bit addressing if there are already 250 two address instruction how many one address instruction can be formulated
1 votes
1 votes
1 answer
4