Parity Check (odd/even)

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Parity Check (odd/even)

Post by Art »

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.
User avatar
groepaz
Posts: 305
Joined: Thu Sep 01, 2005 7:44 am
Contact:

Post by groepaz »

if(value&1){ /* odd */ }else{ /* even */}
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

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..
User avatar
groepaz
Posts: 305
Joined: Thu Sep 01, 2005 7:44 am
Contact:

Post by groepaz »

yes yes and yes :=) & is bitwise AND, so this masks the LSB (which ofcourse is bit0, not bit1).
Samstag
Posts: 5
Joined: Thu Nov 24, 2005 10:41 am

Post by Samstag »

groepaz wrote:if(value&1){ /* odd */ }else{ /* even */}
That's not a parity check, though. You'd want something more like:

Code: Select all

byte parity = 0;
for&#40;i = 0; i < 8; i++&#41;
&#123;
    if&#40;value & 1&#41;
        parity = !parity;    // toggle parity status
    value = value >> 1;  // rotate bits right
&#125;
Untested.
User avatar
groepaz
Posts: 305
Joined: Thu Sep 01, 2005 7:44 am
Contact:

Post by groepaz »

well the original question wasnt about parity, but "What is the quickest and easiest syntax in C for checking if a byte value
is odd or even?" :=)
Samstag
Posts: 5
Joined: Thu Nov 24, 2005 10:41 am

Post by Samstag »

Ah, well that's certainly true :)
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

That's what I meant.
One of the web definitions for parity:
"The quality of being either odd or even"
siberianstar
Posts: 70
Joined: Thu Jun 22, 2006 9:24 pm

Post by siberianstar »

"Parity check" in informatic language is used to define the algorithm to check data errors on trasmission between pc and serial ports.
Post Reply