4 ms·
Your function is incorrect. Consider the case a = b = 0x80000000.
by pbsd 12y ago
Your function is incorrect. Consider the case a = b = 0x80000000.
- professorTuring 12y agoI might be missing something, but I have tested it and it gives me "0" and no overflow. edit: Ok, I do missed one spot (my mind tricked me), the answer shouldn't be 0.
- professorTuring 12y agoOk, I think this should do the trick: return((a ^ b) | (a ^ (*rp)) < 0) ? 1 : 0; doesn't it?
- pbsd 12y agoNot yet. Consider a = -1, b = 1. Due to the OR, any a and b with different signs will return overflow.
- professorTuring 12y agoOk this is my dumbest time in life xD (I just directly didn't take into consideration that part because it never has overflow...)...
- barrkel 12y agoMost people don't appreciate how complicated C's mix of signed and unsigned is, especially when undefined behaviour of signed overflow is included.
- professorTuring 12y agoDefinitely. Child, this is why I always add unit tests.