Even Parity

Discussion in 'C++' started by nick.stefanov@gmail.com, Nov 28, 2005.

  1. Guest

    I am trying to compute an even parity bit for a variable of type
    char[]. Are there any C++, C# examples on how to first compute the bit
    and then check it. Thank you very much ahead of time.

    Nick
     
    , Nov 28, 2005
    #1
    1. Advertising

  2. Guest

    wrote:
    > I am trying to compute an even parity bit for a variable of type
    > char[]. Are there any C++, C# examples on how to first compute the bit
    > and then check it. Thank you very much ahead of time.


    Try the >> operator.
     
    , Nov 28, 2005
    #2
    1. Advertising

  3. wrote:
    > I am trying to compute an even parity bit for a variable of type
    > char[]. Are there any C++, C# examples on how to first compute the
    > bit and then check it. Thank you very much ahead of time.


    If there are examples, they would be on the 'Net, don't you think?
    Have you tried looking for them there? Besides, what does C# have
    to do with this newsgroup?
     
    Victor Bazarov, Nov 28, 2005
    #3
  4. Rolf Magnus Guest

    wrote:

    > I am trying to compute an even parity bit for a variable of type
    > char[]. Are there any C++, C# examples on how to first compute the bit
    > and then check it. Thank you very much ahead of time.


    This should give you an idea of how to do it:

    #include <iostream>
    #include <limits>

    int main()
    {
    unsigned char x = 0x36;

    for (int i = 0; i < std::numeric_limits<unsigned char>::digits; ++i)
    {
    bool bit = x & (1 << i);
    std::cout << "bit " << i << " is " << bit << '\n';
    }
    }
     
    Rolf Magnus, Nov 28, 2005
    #4
  5. wrote:
    > I am trying to compute an even parity bit for a variable of type
    > char[]. Are there any C++, C# examples on how to first compute the bit
    > and then check it. Thank you very much ahead of time.
    >
    > Nick
    >

    This code will return true if an even parity bit is required

    bool ParityBit(char inp)
    {
    bool Parity = false;
    for (;inp;inp &= (inp-1))
    {
    Parity ^= true;
    }
    return Parity;
    }


    Note by setting Parity to true to begin with will result in
    returning true for odd parity.

    JB
     
    n2xssvv g02gfr12930, Nov 28, 2005
    #5
  6. red floyd Guest

    Victor Bazarov wrote:
    > wrote:
    >
    >>I am trying to compute an even parity bit for a variable of type
    >>char[]. Are there any C++, C# examples on how to first compute the
    >>bit and then check it. Thank you very much ahead of time.

    >
    >
    > If there are examples, they would be on the 'Net, don't you think?
    > Have you tried looking for them there? Besides, what does C# have
    > to do with this newsgroup?
    >
    >


    Actually, it's probably cheapest just to use a lookup table with 256
    entries.
     
    red floyd, Nov 28, 2005
    #6
  7. red floyd wrote:
    > Victor Bazarov wrote:
    >
    >> wrote:
    >>
    >>> I am trying to compute an even parity bit for a variable of type
    >>> char[]. Are there any C++, C# examples on how to first compute the
    >>> bit and then check it. Thank you very much ahead of time.

    >>
    >>
    >>
    >> If there are examples, they would be on the 'Net, don't you think?
    >> Have you tried looking for them there? Besides, what does C# have
    >> to do with this newsgroup?
    >>
    >>

    >
    > Actually, it's probably cheapest just to use a lookup table with 256
    > entries.


    256 entries in a table for "a variable of type char[]"? How do you
    figure?

    V
     
    Victor Bazarov, Nov 28, 2005
    #7
  8. red floyd Guest

    Victor Bazarov wrote:
    > red floyd wrote:
    >>
    >> Actually, it's probably cheapest just to use a lookup table with 256
    >> entries.

    >
    >
    > 256 entries in a table for "a variable of type char[]"? How do you
    > figure?
    >
    > V


    I meant it was faster to use a lookup than to use the shifting
    methods mentioned above.

    #include <algorithm>

    struct parity
    {
    unsigned char operator()(unsigned char c) const
    {
    static const unsigned char parity_table[] = {
    // FILL THIS IN
    };
    return parity_table[c];
    }
    };

    unsigned char get_parity(unsigned char buf[], int len)
    {
    return std::accumulate(buf, buf+len, 0, parity());
    }
     
    red floyd, Nov 28, 2005
    #8
  9. red floyd wrote:
    > Victor Bazarov wrote:
    >
    >> red floyd wrote:
    >>
    >>>
    >>> Actually, it's probably cheapest just to use a lookup table with 256
    >>> entries.

    >>
    >>
    >>
    >> 256 entries in a table for "a variable of type char[]"? How do you
    >> figure?
    >>
    >> V

    >
    >
    > I meant it was faster to use a lookup than to use the shifting
    > methods mentioned above.


    And I meant to point out that you'd assumed the OP was asking about the
    unsigned char type, on a system that has CHAR_BITS == 8. The OP did no
    such thing judging from the post alone.

    > #include <algorithm>
    >
    > struct parity
    > {
    > unsigned char operator()(unsigned char c) const
    > {
    > static const unsigned char parity_table[] = {
    > // FILL THIS IN


    How do you "FILL THIS IN"? A bunch of #ifdef's? What if the size of the
    'unsigned char' is 16 bits? What if it's 32 bits? Please do not rush to
    reply.

    > };
    > return parity_table[c];
    > }
    > };
    >
    > unsigned char get_parity(unsigned char buf[], int len)
    > {
    > return std::accumulate(buf, buf+len, 0, parity());
    > }


    V
     
    Victor Bazarov, Nov 28, 2005
    #9
  10. red floyd Guest

    Victor Bazarov wrote:
    >
    > And I meant to point out that you'd assumed the OP was asking about the
    > unsigned char type, on a system that has CHAR_BITS == 8. The OP did no
    > such thing judging from the post alone.
    >


    Good point about CHAR_BITS. I stand corrected. However, when
    generating parity, the size of the data item being paritized(?) is
    known, and depending on size, a lookup table can definitely be more
    efficient *and* simpler.
     
    red floyd, Nov 28, 2005
    #10
  11. Guest

    red floyd wrote:
    > Victor Bazarov wrote:
    > >
    > > And I meant to point out that you'd assumed the OP was asking about the
    > > unsigned char type, on a system that has CHAR_BITS == 8. The OP did no
    > > such thing judging from the post alone.
    > >

    >
    > Good point about CHAR_BITS. I stand corrected. However, when
    > generating parity, the size of the data item being paritized(?) is
    > known, and depending on size, a lookup table can definitely be more
    > efficient *and* simpler.


    A combination of both is even better in many cases. For example, you
    have a 63 bit data item you want to do a parity or a parity check on.
    2^63 is big, 2^8 is not overly so. So, bit shift by 8 and do a lookup.
    Tally the results... Rather similar to the "first bit" algorithm
    often used in bitboard chess programs.
     
    , Nov 29, 2005
    #11
  12. Guest

    Thank you very much for all of your input.
     
    , Nov 29, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ed

    Parity Check

    Ed, Sep 30, 2004, in forum: VHDL
    Replies:
    18
    Views:
    10,810
    yassen
    Oct 6, 2009
  2. Stan Goodman

    Even older fart, even newer newbie

    Stan Goodman, Jul 3, 2003, in forum: Java
    Replies:
    11
    Views:
    701
    Stan Goodman
    Jul 4, 2003
  3. snacktime

    Set parity of a string

    snacktime, Jan 23, 2005, in forum: Python
    Replies:
    8
    Views:
    388
    Peter Hansen
    Jan 26, 2005
  4. Petr Jakes
    Replies:
    2
    Views:
    5,127
    Petr Jakes
    Nov 12, 2005
  5. shakeelsultan

    odd parity checker FSM

    shakeelsultan, Oct 27, 2006, in forum: VHDL
    Replies:
    0
    Views:
    813
    shakeelsultan
    Oct 27, 2006
Loading...

Share This Page