Determine if x-y will overflow in c
by Portal from LinuxQuestions.org on (#4QFGM)
I have the following homework question:
Code:Write a function with the following prototype:
/* Determine whether arguments can be subtracted without overflow */
int tsub_ok(int x, int y);
This function should return 1 if the computation x - y does not overflow(I am only allowed to use bit-level and logic operation, shifts, and c constants; the code should be straight line with no conditionals, loops, division, modulus, multiplication, or relative comparison operators. Assume 2's complement for signed binaries with w-bit word size)
I want to check for positive overflow and negative overflow respectively and do a bitwise-or. But I don't know how to do each respectively. Any hints?


Code:Write a function with the following prototype:
/* Determine whether arguments can be subtracted without overflow */
int tsub_ok(int x, int y);
This function should return 1 if the computation x - y does not overflow(I am only allowed to use bit-level and logic operation, shifts, and c constants; the code should be straight line with no conditionals, loops, division, modulus, multiplication, or relative comparison operators. Assume 2's complement for signed binaries with w-bit word size)
I want to check for positive overflow and negative overflow respectively and do a bitwise-or. But I don't know how to do each respectively. Any hints?