Integer arithmetic: Add 1 to UINT_MAX and divide by n without overflow
问题 Is there a way to compute the result of ((UINT_MAX+1)/x)*x-1 in C without resorting to unsigned long (where x is unsigned int )? (respective "without resorting to unsigned long long " depending on architecture.) 回答1: It is rather simple arithmetic: ((UINT_MAX + 1) / x) * x - 1 = ((UINT_MAX - x + x + 1) / x) * x - 1 = ((UINT_MAX - x + 1) / x + 1) * x - 1 = (UINT_MAX - x + 1) / x) * x + (x - 1) 回答2: With integer divisions we have the following equivalence (y/x)*x == y - y%x So we have ((UINT