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

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -