Re: Parity check of a word
- From: Lawrence Kirby <lknews@xxxxxxxxxxxxxxx>
- Date: Wed, 31 Aug 2005 14:26:30 +0100
On Mon, 29 Aug 2005 01:37:27 -0700, Krishanu Debnath wrote:
....
> Following code works for even parity scheme.
>
> int parity (unsigned x)
> {
> unsigned y;
>
> y = x ^ (x >> 1);
> y = y ^ (y >> 2);
> y = y ^ (y >> 4);
> y = y ^ (y >> 8);
> y = y ^ (y >>16);
> return y & 1;
> }
However it assumes that int/unsigned int are wider than 16 bits
which is not portable; if unsigned is 16 bits wide then y >> 16 has
undefined behaviour. If you want an unsigned type can can store at least
32 bits use unsigned long, or maybe uint_least32_t in C99.
Lawrence
.
- References:
- Parity check of a word
- From: Herbert Haas
- Re: Parity check of a word
- From: Ben Pfaff
- Re: Parity check of a word
- From: kernelxu
- Re: Parity check of a word
- From: Krishanu Debnath
- Parity check of a word
- Prev by Date: Re: Code consolidation
- Next by Date: Re: MACRO exclusion
- Previous by thread: Re: Parity check of a word
- Next by thread: Re: Parity check of a word
- Index(es):
Relevant Pages
|