They are the same. Here's a proof:
First note the identity (A + B) mod C = (A mod C + B mod C) mod C
Let's restate the problem by regarding a & 255
as standing in for a % 256
. This is true since a
is unsigned.
So (a + (b & 255)) & 255
is (a + (b % 256)) % 256
This is the same as (a % 256 + b % 256 % 256) % 256
(I've applied the identity stated above: note that mod
and %
are equivalent for unsigned types.)
This simplifies to (a % 256 + b % 256) % 256
which becomes (a + b) % 256
(reapplying the identity). You can then put the bitwise operator back to give
(a + b) & 255
completing the proof.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…