3 ms·
I hate to be that guy, but isn't the behavior for bool f(unsigned int x, unsigned int y) { return (x+y == y+x); } undefined when x+y or y=x overflow? So no
by eschneider 5y ago
I hate to be that guy, but isn't the behavior for
bool f(unsigned int x, unsigned int y) {
return (x+y == y+x);
}
undefined when x+y or y=x overflow? So no, it's NOT guaranteed to never return false. :/
- sva_ 5y agoOnly if you assume x+y must be an unsigned int as well. They didn't specify the language, so you can't know that, I'd say. In a theorem prover you could prove this statement without doubt, but it's a bit weird they specified the unsigned int, to be fair.
- AdamH12113 5y agoUnsigned integer overflow is defined in C. Signed integer overflow is not, although in practice the CPU can do it just fine.