这可以。根据 C99 规范,对于 unsigned long 操作,结果是相同的,但减少了模 2 32 (§6.2.5):
A computation involving unsigned operands can never overflow, because a result
that cannot be represented by the resulting unsigned integer type is reduced
modulo the number that is one greater than the largest value that can be
represented by the resulting type.
所以这种行为实际上并不是“溢出”,但为了简单起见,我将在这个答案中称之为。因为对于模算术,我们有
a1 ≡ b1 (mod m)
a2 ≡ b2 (mod m)
暗示
a1 + a2 ≡ b1 + b2 (mod m)
我们有
Next * a ≡ k (mod 2^32)
k
“溢出”在哪里Next * a
。所以自从M = 2^32
,
Next * a + c ≡ k + c (mod M)
有“溢出”的结果在模运算下等价于没有“溢出”的结果,所以公式没问题。一旦我们减少模M = 2^32
,它会给出相同的结果。