c - Integer arithmetic: Add 1 to UINT_MAX and divide by n without overflow -
is there way compute result of ((uint_max+1)/x)*x-1
in c without resorting unsigned long
(where x
unsigned int
)? (respective "without resorting unsigned long long
" depending on architecture.)
it 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)
Comments
Post a Comment