0

In C++ i try to use modulo operator for two unsigned int variables like in Marsaglia's multiply with carry algorithm. The results seem right, but i'm not sure about the limitations of modulo.

m_upperBits = (36969 * (m_upperBits & 65535) + (m_upperBits >> 16))<<16;
m_lowerBits = 18000 * (m_lowerBits & 65535) + (m_lowerBits >> 16);
unsigned int sum = m_upperBits + m_lowerBits;  /* 32-bit result */
unsigned int mod = (max-min+1);
int result=min+sum%mod;
Meldryt
  • 75
  • 1
  • 10

1 Answers1

0

Reference: C++03 paragraph 5.6 clause 4

The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined; otherwise (a/b)*b + a%b is equal to a. If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined.

I don't see any limitations of modulo in c.

Rishit Sanmukhani
  • 2,159
  • 16
  • 26