Discuss the development of new homebrew software, tools and libraries.
Moderators: cheriff , TyRaNiD
Art
Posts: 642 Joined: Wed Nov 09, 2005 8:01 am
Post
by Art » Wed Aug 16, 2006 2:25 pm
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.
groepaz
Posts: 305 Joined: Thu Sep 01, 2005 7:44 am
Contact:
Post
by groepaz » Wed Aug 16, 2006 3:01 pm
if(value&1){ /* odd */ }else{ /* even */}
Art
Posts: 642 Joined: Wed Nov 09, 2005 8:01 am
Post
by Art » Wed Aug 16, 2006 3:35 pm
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..
groepaz
Posts: 305 Joined: Thu Sep 01, 2005 7:44 am
Contact:
Post
by groepaz » Wed Aug 16, 2006 3:59 pm
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 » Wed Aug 16, 2006 9:15 pm
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(i = 0; i < 8; i++)
{
if(value & 1)
parity = !parity; // toggle parity status
value = value >> 1; // rotate bits right
}
Untested.
groepaz
Posts: 305 Joined: Thu Sep 01, 2005 7:44 am
Contact:
Post
by groepaz » Wed Aug 16, 2006 9:25 pm
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 » Wed Aug 16, 2006 9:32 pm
Ah, well that's certainly true :)
Art
Posts: 642 Joined: Wed Nov 09, 2005 8:01 am
Post
by Art » Thu Aug 17, 2006 6:55 am
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 » Thu Aug 17, 2006 7:52 am
"Parity check" in informatic language is used to define the algorithm to check data errors on trasmission between pc and serial ports.