How to Easily and Consistently Round Up with Integer Division

I’ve encountered this in different languages for different purposes. The first time was some assembler code that calculated how many total bytes it would take to hold an arbitrary number of bits. The problem is it’s usually coded the way we think about the problem: divide, then if there’s a remainder, add 1 to the quotient.

A quick, easy way without any special checking, is to add the (divisor-1) to the dividend before doing the division. In the bits to bytes example, I changed the code to do it this way