int non_smaller_int_divisible_by_16(int x) { return (x + 15) & ~15; }
Since 16 is the power of two, you can use binary masking - add 15 so we get the next highest multiplier and a mask with a bitwise inversion of 15 to clear the lower bits.
Edit:
It is not clear what you want to do with negative numbers - both your and my code will be rounded up to more positive values ββ(i.e. negative numbers will decrease). If negative values ββdo not make sense in your program, it would be better to use an unsigned type.
Finally, you might be interested to watch Bit Twiddling Hacks , which is an excellent collection of some really smart (if often extremely obscure) tricks along these lines.
source share