| BitTwiddle - Your daily bit twiddle fortune. | |
| ___[ Contribute ] | |
| Want to share your BitTwiddles? | |
| Show them to use on irc.bitreich.org/#bitreich-en! | |
| ___[ Twiddle ] | |
| Compute parity by lookup table | |
| static const bool ParityTable256[256] = | |
| { | |
| # define P2(n) n, n^1, n^1, n | |
| # define P4(n) P2(n), P2(n^1), P2(n^1), P2(n) | |
| # define P6(n) P4(n), P4(n^1), P4(n^1), P4(n) | |
| P6(0), P6(1), P6(1), P6(0) | |
| }; | |
| unsigned char b; // byte value to compute the parity of | |
| bool parity = ParityTable256[b]; | |
| // OR, for 32-bit words: | |
| unsigned int v; | |
| v ^= v >> 16; | |
| v ^= v >> 8; | |
| bool parity = ParityTable256[v & 0xff]; | |
| // Variation: | |
| unsigned char * p = (unsigned char *) &v; | |
| parity = ParityTable256[p[0] ^ p[1] ^ p[2] ^ p[3]]; | |
| Randal E. Bryant encouraged the addition of the (admittedly) obvious last | |
| variation with variable p on May 3, 2005. Bruce Rawles found a typo in an | |
| instance of the table variable's name on September 27, 2005, and he received a | |
| $10 bug bounty. On October 9, 2006, Fabrice Bellard suggested the 32-bit | |
| variations above, which require only one table lookup; the previous version had | |
| four lookups (one per byte) and were slower. On July 14, 2009 Hallvard Furuseth | |
| suggested the macro compacted table. | |
| ================================ | |
| The twiddles are from various sources. See: | |
| Fortune twiddles. | |
| Twiddle scripts. | |
| << back to bitreich.org |