C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Even Parity

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
nick.stefanov@gmail.com
Guest





PostPosted: Mon Nov 28, 2005 5:38 am    Post subject: Even Parity Reply with quote



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

Back to top
roberts.noah@gmail.com
Guest





PostPosted: Mon Nov 28, 2005 5:43 am    Post subject: Re: Even Parity Reply with quote




nick.stefa... (AT) gmail (DOT) com wrote:
Quote:
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.


Back to top
Victor Bazarov
Guest





PostPosted: Mon Nov 28, 2005 5:43 am    Post subject: Re: Even Parity Reply with quote



[email]nick.stefanov (AT) gmail (DOT) com[/email] wrote:
Quote:
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?



Back to top
Rolf Magnus
Guest





PostPosted: Mon Nov 28, 2005 9:45 am    Post subject: Re: Even Parity Reply with quote

[email]nick.stefanov (AT) gmail (DOT) com[/email] wrote:

Quote:
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 {
bool bit = x & (1 << i);
std::cout << "bit " << i << " is " << bit << 'n';
}
}


Back to top
n2xssvv g02gfr12930
Guest





PostPosted: Mon Nov 28, 2005 10:26 am    Post subject: Re: Even Parity Reply with quote

[email]nick.stefanov (AT) gmail (DOT) com[/email] wrote:
Quote:
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

Back to top
red floyd
Guest





PostPosted: Mon Nov 28, 2005 4:43 pm    Post subject: Re: Even Parity Reply with quote

Victor Bazarov wrote:
Quote:
nick.stefanov (AT) gmail (DOT) com 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.

Back to top
Victor Bazarov
Guest





PostPosted: Mon Nov 28, 2005 5:38 pm    Post subject: Re: Even Parity Reply with quote

red floyd wrote:
Quote:
Victor Bazarov wrote:

[email]nick.stefanov (AT) gmail (DOT) com[/email] 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

Back to top
red floyd
Guest





PostPosted: Mon Nov 28, 2005 6:55 pm    Post subject: Re: Even Parity Reply with quote

Victor Bazarov wrote:
Quote:
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());
}

Back to top
Victor Bazarov
Guest





PostPosted: Mon Nov 28, 2005 7:34 pm    Post subject: Re: Even Parity Reply with quote

red floyd wrote:
Quote:
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.

Quote:
#include
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.

Quote:
};
return parity_table[c];
}
};

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

V

Back to top
red floyd
Guest





PostPosted: Mon Nov 28, 2005 9:45 pm    Post subject: Re: Even Parity Reply with quote

Victor Bazarov wrote:
Quote:

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.

Back to top
roberts.noah@gmail.com
Guest





PostPosted: Tue Nov 29, 2005 4:41 am    Post subject: Re: Even Parity Reply with quote


red floyd wrote:
Quote:
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.


Back to top
nick.stefanov@gmail.com
Guest





PostPosted: Tue Nov 29, 2005 4:42 pm    Post subject: Re: Even Parity Reply with quote

Thank you very much for all of your input.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.