| View previous topic :: View next topic |
| Author |
Message |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Wed Aug 16, 2006 2:25 pm Post subject: Parity Check (odd/even) |
|
|
Hi,
I've run into trouble with an encryption routine I'm trying to clone.
What is the quickest and easiest syntax in C for checking if a byte value
is odd or even?
I tried searching on Google, but mention "Parity" and it's all about error checking.
Thanx, Art. |
|
| Back to top |
|
 |
groepaz

Joined: 01 Sep 2005 Posts: 305
|
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Wed Aug 16, 2006 3:35 pm Post subject: |
|
|
Thanx groepaz :)
Is that checking the LSB?
why is bit0 called bit 1?
I guess it all depends on what the "&" is if it's an and I might learn something here.. |
|
| Back to top |
|
 |
groepaz

Joined: 01 Sep 2005 Posts: 305
|
|
| Back to top |
|
 |
Samstag
Joined: 24 Nov 2005 Posts: 5
|
Posted: Wed Aug 16, 2006 9:15 pm Post subject: |
|
|
| groepaz wrote: | | if(value&1){ /* odd */ }else{ /* even */} |
That's not a parity check, though. You'd want something more like:
| Code: | byte parity = 0;
for(i = 0; i < 8; i++)
{
if(value & 1)
parity = !parity; // toggle parity status
value = value >> 1; // rotate bits right
} |
Untested. |
|
| Back to top |
|
 |
groepaz

Joined: 01 Sep 2005 Posts: 305
|
|
| Back to top |
|
 |
Samstag
Joined: 24 Nov 2005 Posts: 5
|
Posted: Wed Aug 16, 2006 9:32 pm Post subject: |
|
|
| Ah, well that's certainly true :) |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Thu Aug 17, 2006 6:55 am Post subject: |
|
|
That's what I meant.
One of the web definitions for parity:
"The quality of being either odd or even" |
|
| Back to top |
|
 |
siberianstar
Joined: 22 Jun 2006 Posts: 70
|
Posted: Thu Aug 17, 2006 7:52 am Post subject: |
|
|
| "Parity check" in informatic language is used to define the algorithm to check data errors on trasmission between pc and serial ports. |
|
| Back to top |
|
 |
|