Mail Archives: djgpp/2000/03/02/15:17:50
Chris Mears wrote:
> That Trancelucid <trancelucid AT videotron DOT ca> really knows where his
> towel is. On Thu, 02 Mar 2000 06:07:49 -0500, he wrote:
>
> >DAVID JACOBS wrote:
> >>
> >> What is the fastest way to check wether an int/long is odd or even?
> >> I've checked my C/C++ manual, but I didn't find anything....
> >
> >Just mask the bit 0..
> >
> >if(i & 0x01) {
> > printf("odd");
> >}
> >else {
> > printf("even");
> >}
>
> I don't know how well this will work with signed integers.
It should work fine with signed integers....
-1 = 0xFFFFFFFF (lowest bit set)
-2 = 0xFFFFFFFE (lowest bit not set)
> I believe
> the fastest[*] way is:
>
> #define EVEN(x) (((x) % 2) == 0)
> #define ODD(x) (((x) % 2) != 0)
A literal "%" requires a divide (which is slow). I suspect that with
optimizations on it would be reduced to (x&1) anyways.
--
(\/) Rolf Campbell (\/)
- Raw text -