16

A value has even parity if it has an even number of 1 bits. A value has an odd parity if it has an odd number of 1 bits. For example, 0110 has even parity, and 1110 has odd parity.

I have to return 1 if x has even parity.

int has_even_parity(unsigned int x) {
    return 
}
nalzok
  • 13,395
  • 18
  • 64
  • 118
Manuel
  • 205
  • 1
  • 2
  • 9
  • 1
    Welcome to SO. Please read [ask] and [help] on how to ask a question. Go read about bit shifting in C. – OldProgrammer Feb 07 '14 at 02:11
  • 4
    @OldProgrammer Is that called for? It's not clear to me that the OP should be expected to know to search for that. It's a fair and clear question. – TypeIA Feb 07 '14 at 02:16
  • Possible duplicate of [Bit parity code for odd number of bits](http://stackoverflow.com/questions/7530599/bit-parity-code-for-odd-number-of-bits) – Troyseph Oct 27 '15 at 09:58
  • 1
    See here: [stackoverflow.com/questions/21589674/even-parity-of-a-unsigned-int](http://stackoverflow.com/questions/21589674/even-parity-of-a-unsigned-int). – Troyseph Oct 27 '15 at 10:03

9 Answers9

89
x ^= x >> 16;
x ^= x >> 8;
x ^= x >> 4;
x ^= x >> 2;
x ^= x >> 1;
return (~x) & 1;

Assuming you know ints are 32 bits.


Let's see how this works. To keep it simple, let's use an 8 bit integer, for which we can skip the first two shift/XORs. Let's label the bits a through h. If we look at our number we see:

( a b c d e f g h )


The first operation is x ^= x >> 4 (remember we're skipping the first two operations since we're only dealing with an 8-bit integer in this example). Let's write the new values of each bit by combining the letters that are XOR'd together (for example, ab means the bit has the value a xor b).

( a b c d e f g h ) xor ( 0 0 0 0 a b c d )

The result is the following bits:

( a b c d ae bf cg dh )


The next operation is x ^= x >> 2:

( a b c d ae bf cg dh ) xor ( 0 0 a b c d ae bf )

The result is the following bits:

( a b ac bd ace bdf aceg bdfh )

Notice how we are beginning to accumulate all the bits on the right-hand side.


The next operation is x ^= x >> 1:

( a b ac bd ace bdf aceg bdfh ) xor ( 0 a b ac bd ace bdf aceg )

The result is the following bits:

( a ab abc abcd abcde abcdef abcdefg abcdefgh )


We have accumulated all the bits in the original word, XOR'd together, in the least-significant bit. So this bit is now zero if and only if there were an even number of 1 bits in the input word (even parity). The same process works on 32-bit integers (but requires those two additional shifts that we skipped in this demonstration).

The final line of code simply strips off all but the least-significant bit (& 1) and then flips it (~x). The result, then, is 1 if the parity of the input word was even, or zero otherwise.

TypeIA
  • 16,427
  • 1
  • 33
  • 47
  • 3
    @cguedel Added a pretty lengthy demonstration/walkthrough above. Hope it helps. – TypeIA Feb 25 '14 at 14:48
  • The final line of code flips bits and then strips the other bits (your explanation has those two the other way around) – M.M Aug 17 '17 at 04:13
  • 1
    the second to the last value in the result should be ```abcdef```(without the h) – Jay M Mar 20 '20 at 12:11
9

GCC has built-in functions for this:

Built-in Function: int __builtin_parity (unsigned int x)

Returns the parity of x, i.e. the number of 1-bits in x modulo 2.

and similar functions for unsigned long and unsigned long long.

I.e. this function behaves like has_odd_parity. Invert the value for has_even_parity.

These should be the fastest alternative on GCC. Of course its use is not portable as such, but you can use it in your implementation, guarded by a macro for example.

  • Instead of reinventing the wheel in different shapes and sizes I think this is the best approach. – faruk13 Jul 05 '19 at 17:05
8

The following answer was unashamedly lifted directly from Bit Twiddling Hacks By Sean Eron Anderson, seander@cs.stanford.edu

Compute parity of word with a multiply

The following method computes the parity of the 32-bit value in only 8 operations >using a multiply.

unsigned int v; // 32-bit word
v ^= v >> 1;
v ^= v >> 2;
v = (v & 0x11111111U) * 0x11111111U;
return (v >> 28) & 1;

Also for 64-bits, 8 operations are still enough.

unsigned long long v; // 64-bit word
v ^= v >> 1;
v ^= v >> 2;
v = (v & 0x1111111111111111UL) * 0x1111111111111111UL;
return (v >> 60) & 1;

Andrew Shapira came up with this and sent it to me on Sept. 2, 2007.

Troyseph
  • 4,731
  • 3
  • 38
  • 58
6

Try:

int has_even_parity(unsigned int x){
    unsigned int count = 0, i, b = 1;

    for(i = 0; i < 32; i++){
        if( x & (b << i) ){count++;}
    }

    if( (count % 2) ){return 0;}

    return 1;
}

valter

2

To generalise @TypelA's answer for any architecture:

int has_even_parity(unsigned int x) 
{
    unsigned char shift=1;
    while (shift < (sizeof(x)*8))
    {
            x ^= (x>>shift);
            shift<<=1;
    }
    return !(x & 0x1);
}
Rishav Ambasta
  • 484
  • 1
  • 9
  • 26
1

The main idea is this. Unset the rightmost '1' bit by using x & ( x - 1 ). Lets say x = 13(1101) and the operation of x & ( x - 1 ) is 1101 & 1100 which is 1100, notice that the rightmost set bit is converted to 0.

Now x is 1100. The operation of x & ( x - 1 ) i.e 1100 & 1011 is 1000. Notice that the original x is 1101 and after two operations of x & (x - 1) the x is 1000, i.e two set bits are removed after two operations. If after odd number of operations, the x becomes zero, then its a odd parity, else its a even parity.

madhu sudhan
  • 157
  • 13
  • Use the following link if you need more explanation and code:[geeks for geeks link](http://www.geeksforgeeks.org/write-a-c-program-to-find-the-parity-of-an-unsigned-integer/) – madhu sudhan Aug 27 '16 at 07:13
1

Here's a one line #define that does the trick for a char:

#define PARITY(x) ((~(x ^= (x ^= (x ^= x >> 4) >> 2) >> 1)) & 1) /* even parity */

int main()
{
    char x=3;
    printf("parity = %d\n", PARITY(x));
}

It's portable as heck and easily modified to work with bigger words (16, 32 bit). It's important to note also, using a #define speeds the code up, each function call requires time to push the stack and allocate memory. Code size doesn't suffer, especially if it's implemented only a few times in your code - the function call might take up as much object code as the XORs.

Admittedly, the same efficiencies may be obtained by using the inline function version of this, inline char parity(char x) {return PARITY(x);} (GCC) or __inline char parity(char x) {return PARITY(x);} (MSVC). Presuming you keep the one line define.

Danny Holstein
  • 150
  • 1
  • 11
0
int parity_check(unsigned x) {
    int parity = 0;
    while(x != 0) {
        parity ^= x;
        x >>= 1;
    }
    return (parity & 0x1);
}
JAL
  • 40,662
  • 22
  • 162
  • 292
  • 10
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – JAL Sep 27 '15 at 22:29
-3

This is quite an old question but I'm posting this for whoever might use it in the future.

I won't add an example of doing it in c since there are enough good answers already.

In case the end result is supposed to be a piece of code that can work (be compiled) with a c program then I suggest the following:

.code

; bool CheckParity(size_t Result)
    CheckParity PROC
    mov     rax, 0
    add     rcx, 0
    jnp     jmp_over
    mov     rax, 1
jmp_over:
    ret
CheckParity ENDP

END

This is a piece of code I'm using to check the parity of calculated results in a 64bit c program compiled using MSVC. You can obviously port it to 32bit or other compilers.

This has the advantage of being much faster than using c and it also leverages the cpus functionality.

What this example does is take as input a parameter (passed in RCX - __fastcall calling convention). It increments it by 0 thus setting the cpus Parity Flag and then setting a variable (RAX) to 0 or 1 if Parity Flag is on or not.

papadp
  • 801
  • 7
  • 14
  • 1
    I cannot get this to compile with GCC on Linux (but I know very little assembly). Would you please provide a hint on how to embed this in a C program? – étale-cohomology May 09 '17 at 23:29
  • 5
    The parity computed by the x86 CPUs is based only on the bits in the lower byte of the destination register, not the entire register. – Michael Petch May 10 '17 at 05:03
  • 1
    Plus `add rcx,0` is an unfortunate choice to update flags, just `test ecx,ecx` would be enough (`ecx`, because parity flag is calculated from 8 bits only any way, so it doesn't matter what part of `rcx` you test, and `ecx` is short machine code). And `jnp` is slower than `setp` without jumps. And usually most of the C compilers will have built-in intrinsic function, which is definitely best (and fastest) choice, to avoid some small bug in 3 lines code... (like demonstrated here). – Ped7g May 10 '17 at 11:27
  • See https://stackoverflow.com/questions/43883473/working-inline-assembly-in-c-for-bit-parity/43929095#43929095 for an analysis of why this sucks even if you fixed it to work for the whole 64 bits, and for much better ways to compute parity on x86. (With benchmarks on Haswell) – Peter Cordes Sep 19 '17 at 05:06