It is important to note that here we are dealing with unsigned ints ( uint32_t ), so -upper_bound does not do what you think. This is actually 2**32 - upper_bound , due to the modular wrapper, and the purpose of this is explained in the comment above (i.e. 2**32 % upper_bound without overflow).
Example:
#include <stdio.h> #include <stdint.h> int main() { uint32_t upper_bound = 42; uint32_t min = -upper_bound % upper_bound; printf("%u -> %u\n", upper_bound, min); return 0; }
gives:
42 -> 4
Live code
source share