Fast ceiling of an integer division in C / C++

For positive numbers

unsigned int x, y, q;

To round up …

q = (x + y - 1) / y;

or (avoiding overflow in x+y)

q = 1 + ((x - 1) / y); // if x != 0

Leave a Comment