473,395 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Byte parity question...

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?

Sep 9 '07 #1
8 5811

"philbo30" <ma******@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
>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?
counting 1s, though possible, but is a slow option.

k=0; for(j=0; j<8; j++)if(i&(1<<j))k++;
even: !(k&1)
odd: k&1
one can use a table, or a bitmap, instead.

nibbles (Even, Odd):
0-E 1-O 2-O 3-E
4-O 5-E 6-E 7-O
8-O 9-E A-E B-O
C-E D-O E-O F-E

so, a bitmask:
static int p_even=0x9669;
static int p_odd=0x6996;

nibble 'i' is even? 'p_even&(1<<i)' or '(p_even>>i)&1'.

and for a byte:
((p_even>>(i&15))&1)==((p_even>>((i>>4)&15))&1)

or, odd:
((p_odd>>(i&15))^(p_odd>>((i>>4)&15)))&1

consequently, we can define a bitmap table, and do a byte like this:
(p_even_bm[i>>4]>>(i&15))&1
(p_odd_bm[i>>4]>>(i&15))&1
or, even faster (but taking a lot more space), is giving an individual value
for every input value:
p_even_tab[i]
p_odd_tab[i]

along with other possibilities...
Sep 10 '07 #2
On Sep 10, 4:13 am, philbo30 <masfe...@gmail.comwrote:
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?
You may use XOR operation.

Sep 10 '07 #3
<ju**********@yahoo.co.ina écrit dans le message de news:
11*********************@50g2000hsm.googlegroups.co m...
On Sep 10, 4:13 am, philbo30 <masfe...@gmail.comwrote:
>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?

You may use XOR operation.
That's an interesting alternative to a memory based lookup table.
Assuming bytes have 8 bits, this code should do:

static inline int byte_parity(unsigned char b) {
b ^= b >4;
b ^= b >2;
b ^= b >1;
return b & 1;
}

This method can be easily generalized to larger unsigned types.

--
Chqrlie
Sep 10 '07 #4
philbo30 wrote:
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?
There is no such function in the standard C library.

If CHAR_BIT is 8, you could use this table:

static unsigned char parity[] =
{
0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,
1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,
1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,
0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,1,0,
1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,
0,0,1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,
0,1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,
0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,
1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,1,
0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,
0,1,0,1,1,0
};

where parity[octet] gives the bit count. Yes, the above table was
generated by simply counting the 1s.

--
Tor <torust [at] online [dot] no>
Sep 10 '07 #5
Tor Rustad wrote:
where parity[octet] gives the bit count.
ITYM:

"where parity[octet] gives the parity of the octet."
--
Tor <torust [at] online [dot] no>
Sep 10 '07 #6
In article <H9*********************@telenor.com>,
Tor Rustad <to********@hotmail.comwrote:
>where parity[octet] gives the bit count.
>ITYM:

"where parity[octet] gives the parity of the octet."
Maybe he just forgot to say "modulo 2" at the end.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 10 '07 #7
Charlie Gordon wrote:
<ju**********@yahoo.co.ina écrit dans le message de news:
11*********************@50g2000hsm.googlegroups.co m...
>On Sep 10, 4:13 am, philbo30 <masfe...@gmail.comwrote:
>>Is there a C function available to determine byte parity or is the
usual course to just count the 1s?

Assuming bytes have 8 bits, this code should do:

static inline int byte_parity(unsigned char b) {
b ^= b >4;
b ^= b >2;
b ^= b >1;
return b & 1;
}

This method can be easily generalized to larger unsigned types.
This generalization, for a byte, should work for all sizes of bytes.
Most compilers will eliminate the while loop for 8-bit byte targets.

/* Function: parity
** Description: Return the parity of the argument.
*/
int /* parity of b: 1 if number of 1 bits is odd,
** 0 otherwise. */
parity (unsigned char b) {
while (b 0xff) {
b = (b >8) ^ (b & 0xff);
}
b ^= b >4;
b ^= b >2;
return ((b >1) ^ b) & 1;
}

--
Thad
Sep 11 '07 #8
>On Sep 10, 4:13 am, philbo30 <masfe...@gmail.comwrote:
>>Is there a C function available to determine byte parity or is the
usual course to just count the 1s?
Charlie Gordon wrote:
>Assuming bytes have 8 bits, this code should do:

static inline int byte_parity(unsigned char b) {
b ^= b >4;
b ^= b >2;
b ^= b >1;
return b & 1;
}

This method can be easily generalized to larger unsigned types.
Thad Smith <Th*******@acm.orgwrites:
This generalization, for a byte, should work for all sizes of
bytes. Most compilers will eliminate the while loop for 8-bit byte
targets.
Or one could use preprocessor directives to help compiler a bit:
/* Function: parity
** Description: Return the parity of the argument.
*/
int /* parity of b: 1 if number of 1 bits is odd,
** 0 otherwise. */
parity (unsigned char b) {
#if CHAR_BIT 16
while (b 0xff) {
b = (b >8) ^ (b & 0xff);
}
#elif CHAR_BIT 8
b ^= b >8;
#endif
b ^= b >4;
b ^= b >2;
return ((b >1) ^ b) & 1;
}
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Sep 11 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: snacktime | last post by:
Is there a module that sets the parity of a string? I have an application that needs to communicate with a host using even parity So what I need is before sending the message, convert it from...
15
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...
2
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...
11
by: nick.stefanov | last post by:
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
96
by: david ullua | last post by:
I am reading "Joel on Software" these days, and am in stuck with the question of "how to calculate bitsize of a byte" which is listed as one of the basic interview questions in Joel's book. Anyone...
0
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...
0
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...
5
by: Ken JS | last post by:
How do I find the Parity Check Matrix, when I’m given the generator matrix? e.g. | 1 0 0 1 1 | G = | 0 1 0 1 2 | | 0 0 1 1 3 | Can anybody teach me how to find the Parity Check...
4
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.