I have to make functions that check for overflow in integer addition, subtraction, and unsigned int addition(using only ! ~ | & ^ + >> <<
). I have functions figured out for signed integer addition and subtraction, but I can't figure out how to do one for unsigned int addition.
How would I go about doing this?
Here is the code I have for the 2 functions I have completed:
int twosAddOk(int x, int y){
int z=x+y;
int a=x>>31;
int b=y>>31;
int c=z>>31;
return !!(a^b)|(!(a^c)&!(b^c));
}
int twosSubtractOK(int x, int y){
int z=x+~y+1;
return !(((x^y & x^z))>>31);
}