What is the fastest way for bit operations to calculate a parity?

vls picture vls · Nov 17, 2010 · Viewed 13.9k times · Source

My solution: (for every bit of the input block, there is such a line)

*parity ^= (((x[0] >> 30) & 0x00000001) * 0xc3e0d69f);

All types are uint32. This line takes the second bit of the input x, shifts it to the LSB and sets all other bits to zero. Then, the 32bit parity is XORed with the corresponding parity set for this bit.

I found that this multiplication solution is the fastest way to do this conditional XOR. Is there a faster way?

Answer

The Archetypal Paul picture The Archetypal Paul · Nov 17, 2010

See here for some neat hacks for calculating parity of a word, byte etc