Is it possible to write a bit of C code to perform an arithmetical operation and detect overflow without using exceptions, nor surpressing them, and avoiding the use of assembler?
I was thinking about something like
long ratPerformAdd(a,b long, BOOL *overflow) {
........
}
where either the sum gets returned or the flag overflow gets set?
I'm looking for a platform independant (but 32 bit) operation which I can use on several machines without resorting to assembler.
>> that was not the sort of thing I was looking for
Here's another addition overflow detection approach:
If both a and b are positive, then overflow if and only if
a > LONG_MAX - b
since that
Hmmm, that was not the sort of thing I was looking for, unless one splits the longs into 16 bit quantities and adds them together. Of course I'll need subtraction and multiplication, since division is really no problem.
>> unless one splits the longs into 16 bit quantities and adds them together.
To do the add, just add the two long values and return the overflow status. No need to split into short ints.
0
With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.
For subtraction, just convert to addition and reuse the addition overflow detection scheme.
For multiplication, you have overflow if one arg is INT_MIN and the other is -1.
For other cases, take the absolute values of a and b (call them A and B).
If you have long long type (i.e., a 64 bit integer), then you test for multiplication overflow: long long val64 = A*B;
and see whether there are excess bits using a mask (i.e., long long mask = ~0x7FFFFFFF)
Or, if 64 bit integer is not available, then you have an overflow if A > INT_MAX/B or B > INT_MAX/A
For division, a/b, you have overflow if a == INT_MIN and b = -1. Then a/b = a, but it should be -a.
Sorry about the delay, I'm still at home with the flu, but will be back in the office on Monday. meanwhile I think that someting alongs the lines of ID 35168856 will be the solution.
Sorry, I forgot this question, since I never got around to implementing the code from it.
I have just tried to close this question, but can't. Somehow I'm supposed to generate a cancel auto-close request before I can close it? How complicated!
0
Featured Post
Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.
Summary:
This tutorial covers some basics of pointer, pointer arithmetic and function pointer.
What is a pointer:
A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to useâ€¦