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 11 12290 ni***********@gmail.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?
nick.stefa...@gmail.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.
Try the >> operator. ni***********@gmail.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.
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';
}
} ni***********@gmail.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.
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
Victor Bazarov wrote: ni***********@gmail.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.
red floyd wrote: Victor Bazarov wrote:
ni***********@gmail.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.
256 entries in a table for "a variable of type char[]"? How do you
figure?
V
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 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 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 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.
Thank you very much for all of your input. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Petr Jakes |
last post by:
Hi,
I am trying to set-up communication to the coin change-giver from my
Linux box using the Python code. The change giver uses MDB (Multi Drop
Bus) serial protocol to communicate with the...
|
by: Herbert Haas |
last post by:
Hi everyone,
is there a simple and fast method to check the parity of a word (e. g. a
4-byte integer)? That is I want to know whether the number of ones are
even or odd.
Of course I could do...
|
by: Peter Oliphant |
last post by:
OK, I'm mixing old style with new style, so sue me... : )
Will under old syntax, I'm able to create a SerialPort instance. But, when I
try to convert the Parity proerty to a String*, I get the...
|
by: Loren |
last post by:
I am using parity error checking as I receive data into
the input buffer from the comm port. The data I receive
consists of a parity error, followed by received data,
followed by a parity error. ...
|
by: Rogoras |
last post by:
I use termios.h and have following problem:
My computer comunicates to host computer usig serial RS232.
Host is using parity bit as wake up bit and 8 data bits. In other words parity bit is...
|
by: Radu Crisan |
last post by:
Hi all,
i have this RS232 settings
public static string portName;
public static Int16 baudRate = 19200;
public static Parity parity = Parity.Mark;
public static Int16 dataBits = 8;
public...
|
by: philbo30 |
last post by:
I admit, I didn't spend any time researching this yet so this is the
first step...
Is there a C function available to determine byte parity or is the
usual course to just count the 1s?
|
by: sunfeifei |
last post by:
Your job is to write a C++ program that reads in a matrix and checks if
it has the parity property. If not, your program should check if the parity
property can be established by changing only one...
|
by: =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= |
last post by:
Hello.
It seems a year is all it takes for one's proficiency in C++ to become
too rusty. Professionally, I've been away from the language (and from
programming in general), but I still preserve...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |