Lucky for us, GCC has an intrinsic function _builtin_parity that computes odd parity. More precisely, that’s how odd parity is computed (even parity gives 1 when the number of bits set to 1 is even). If you xor a series of bits together, the result will be one only if there’s an odd number of bits that are 1s, and zero if the number is even. Let’s first convince ourselves that a series of xors are equivalent to computing the parity. This will help us create an efficient implementation-maybe using the compiler intrinsics. Typically, the function is merely a series of exclusive ors, which basically computes the parity of the selected bits. The principle isn’t very complicated: you select a number of bits in the register and use them to compute one bit value, you shift the register by one position and insert the new bit in the vacant spot. While working on a project with a student, I had a look at linear feedback shift registers as a mean of generating (pseudo)random values.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |