Thread: Parity check

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    6

    Parity check

    How can I check the parity of an integer variable (i.e. count the number of '1' bits), without resorting to inline assembly?

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Something like
    Code:
    typedef enum {EVEN = 0, ODD = 1} parity_t;
    
    parity_t FindParity (unsigned int x)
    {
      parity_t parity;
      unsigned int temp = x;
    
      for (parity = EVEN; temp > 0; temp >>= 1) {
        parity ^= (temp & 0x1);
      }
    
      return parity;
    }
    should work...
    DavT
    -----------------------------------------------

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    (i.e. count the number of '1' bits)
    Do you mean to get the total number of set bits? That is to say:

    The parity of the following bit pattern "10100111" would return 5?

    If so, you want:
    Code:
    unsigned char parity( unsigned int O0O )
    {
            int OO0, O00;
            for( OO0 = O00 = 0; OO0 < sizeof(unsigned int)*CHAR_BIT; OO0++ )
                    O00 += (!!(O0O & (1<<OO0)));
            return O00;
    }
    That should suffice.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    6
    int findparity(unsigned int test){
    int count=0;
    int size;

    size = sizeof(test);
    size *= 8;
    size--;

    for(;size>=0;size--){
    if((test>>size) & 0x01)
    count++;
    }
    return count;
    }

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What about the systems with more then 8 bits per byte Karam?

    Quzah you are an evil bastard sometimes

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    6
    how is it possible? everyone knows that

    1 byte=8 bits

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    nope sorry wrong. There are computers out there that have 16, 32, even 64 bit bytes.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >everyone knows that 1 byte=8 bits
    I suppose the people who manufacture architectures where this isn't true haven't heard that yet. It's very likely that every system that you've worked with use an octet for the byte size, but you've not worked with every system that C is portable to, have you?
    My best code is written with the delete key.

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    of course you need limits.h in quzah's code. I love that code quzah

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    6
    Hmm, I was expecting someone to point out a less obvious way of doing it, instead of the pretty straightforward iteration along individual bits (which at first looked rather unelegant to me).

    Say, how does a microprocessor implement this at register level? I know it probably isn't doable in C, but anyway I'm just wondering.

    Quote Originally Posted by quzah
    That should suffice.
    quzah: I hope your screen font is better than mine... it took me a while to check if you were actually right *g*

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I was expecting someone to point out a less obvious way of doing it
    Well, we could suggest something, but that would require an unwarranted assumption about the size of an integer. To count all the set bits, a fairly efficient way that uses a loop is:
    Code:
    unsigned int
    bits(
      unsigned int val
      )
    {
      int n = 0;
    
      while (val) {
        ++n;
        val &= val - 1;
      }
    
      return n;
    }
    Arbitrarily choosing 32 bit integers, you could avoid the loop completely with something like this:
    Code:
    unsigned int
    bits(
      unsigned int val
      )
    {
      val = (val & 0x55555555) + ((val >> 1) & 0x55555555);
      val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
      val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f);
      val = (val & 0x00ff00ff) + ((val >> 8) & 0x00ff00ff);
      val = (val & 0x0000ffff) + ((val >> 16) & 0x0000ffff);
    
      return (int)value;
    }
    Then of course, there's the lookup table option. If you want speed, this is usually a good way to go.
    My best code is written with the delete key.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by doshell
    quzah: I hope your screen font is better than mine... it took me a while to check if you were actually right *g*
    You could have just compiled it. I was going to do this...
    Code:
    unsigned char OxOO( unsigned int Ox0O )
    {
            int OxO0,Ox00;
            for( OxO0=Ox00=0x00;OxO0<sizeof(unsigned int)*CHAR_BIT;OxO0=OxO0+0x01 )
                    Ox00=Ox00+(!!(Ox0O&(0x01<<OxO0)));
            return Ox00;
    }


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Quote Originally Posted by Karam
    how is it possible? everyone knows that

    1 byte=8 bits
    No.
    1 octet = 8 bits
    1 byte = the smallest addressable memory unit in your system. Can be 8 bits or more in C.

    FYI, Texas DSP TMS320C64 bytes (hence char) have 16 bits.
    Emmanuel Delahaye

    "C is a sharp tool"

  14. #14
    .
    Join Date
    Nov 2003
    Posts
    307
    This is really old-fashioned. But works.

    Code:
    #define PARITY ( \
             bits.bitmap.bit0 + \
             bits.bitmap.bit1 + \
             bits.bitmap.bit2 + \
             bits.bitmap.bit3 + \
             bits.bitmap.bit4 + \
             bits.bitmap.bit5 + \
             bits.bitmap.bit6 + \
             bits.bitmap.bit7 )                     
    
    unsigned int parity(const unsigned char mychar){
          struct bmap{              /* bitfields for 8 bit byte  */
                  unsigned bit7:1;  /* 8 bit char  */
                  unsigned bit6:1;  
                  unsigned bit5:1;
                  unsigned bit4:1;
                  unsigned bit3:1;
                  unsigned bit2:1;
                  unsigned bit1:1;
                  unsigned bit0:1;
          };      
          union b_bits{               /* allow moving a byte into the bitfields*/
                  unsigned char ch;
                  struct bmap bitmap;
          } bits;
          bits.ch=mychar;      
          return PARITY;
    }

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There is nothing in C which states that sizeof(struct bmap) == sizeof(unsigned char)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Parity Check Matrix
    By Ken JS in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2007, 03:51 AM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM