From the Bit Twiddling Hacks, I see this trick:
unsigned char b; // byte value to compute the parity of
bool parity =
(((b * 0x0101010101010101ULL) & 0x8040201008040201ULL) % 0x1FF) & 1;
I have spent several hours try to understand why it needs to modulus 0x1FF. How does it work? Why it has to be 0x1FF?
(b * 0x0101010101010101ULL)
makes 8 copies of b and set them in each byte.
((b * 0x0101010101010101ULL) & 0x8040201008040201ULL)
extract different bits from each copy.
But I couldn't figure out the gap in the third step modulus 0x1FF.
The last step is checking whether the last bit is 1 or not.
Any people can help to explain it a little bit? A good example will be very helpful.