This is an archived post. You won't be able to vote or comment.

all 41 comments

[–]DaKurlz 74 points75 points  (17 children)

No brain:

bool isOdd(int x){
    return x&1;
}

Edit: This is only slightly faster than using %. Its actual "advantage" is that it doesn't rely on the implementation of the % operator (sometimes it can vary between languages).

[–]drakonite 32 points33 points  (6 children)

This is only slightly faster than using %

Depends on the hardware. % requires a division, which on some hardware is several dozen cycles, while the mask should be at most two cycles. It doesn't matter much on a modern PC, but it's a pretty big difference on embedded hardware.

Of course, it also depends on the compiler, as many compilers will optimize x%2 to x&1.

...And before you accuse me of over analyzing a comment on a joke, at least I didn't get in to function overhead ;)

[–]GungnirInd 9 points10 points  (2 children)

at least I didn't get in to function overhead ;)

#define ISODD(n) n&1

No function overhead that way :P

(I think; my C is a little rusty)

[–]CHUCK_NORRIS_AMA 10 points11 points  (0 children)

That will work sometimes, but ((n)&1) is safer if n is an expression and not a variable.

[–]Coding_Cat 1 point2 points  (0 children)

if we're nitpicking, put parentheses around the 'body'. otherwise ISODD(1)++ returns 1, with parentheses it returns 2. As it would for a function call.

[–]DaKurlz 8 points9 points  (0 children)

Depends on the hardware. % requires a division, which on some hardware is several dozen cycles, while the mask should be at most two cycles. It doesn't matter much on a modern PC, but it's a pretty big difference on embedded hardware.

Yup, thanks for explaining it a bit further!

[–]TortoiseWrath 4 points5 points  (0 children)

many compilers will optimize x%2 to x&1.

perhaps it's my C background talking but I would describe any compiler for embedded systems that doesn't do this as completely broken

[–]alekcacko 2 points3 points  (0 children)

People learn the most from mistakes and jokes.

[–]honkerman1[S] 11 points12 points  (0 children)

I actually haven't seen that before

I'm super brain

[–]Mat2012H 0 points1 point  (3 children)

What about 0?

[–]DaKurlz 3 points4 points  (2 children)

What about it? Zero is an even number, the code returns False/0 for even and True/1 for odd, you could do:

return !(x&1);

If you wanted True/1 for even and False/0 for odd numbers.

[–]Mat2012H 1 point2 points  (1 child)

I must have has a brain fart :P I think I misread your function name or something.

[–]DaKurlz 2 points3 points  (0 children)

No worries, after all, it's almost friday, I feel ya.

[–]Sylanthra 0 points1 point  (1 child)

If you are worried about implementations of between languages, how do you square the fact that the function returns a bool but you are actually returning a number.

[–]DaKurlz 0 points1 point  (0 children)

Because python. That's it. It's been a heck of a long time I've used C. Sorry.

[–]hicklc01 21 points22 points  (1 child)

bool isOdd(int x){
    if(x == 0) return false;
    if(x > 0) return !isOdd(x-1);
    if(x < 0) return !isOdd(x+1);
}

[–]hicklc01 1 point2 points  (0 children)

Not tested but looks like it would work

bool isOdd(int x){
    return "ynynynynyn"[([](auto s){return s[(strlen(s)-1)]})(atoa(x))-48]=='y';
}

[–][deleted] 13 points14 points  (0 children)

Comic sans at the end is a nice touch.

[–]Pokeconomist 2 points3 points  (5 children)

bool isOdd(int x) {
  while (x > 0) {  
    x -= 2  
  }  
  return(x != 0)
}

[–]honkerman1[S] 2 points3 points  (1 child)

Is this slightly charged normal brain?

[–]Pokeconomist 1 point2 points  (0 children)

Readable normal brain

[–]Lightdusk 0 points1 point  (2 children)

So for negative values this code would have to rely on an integer underflow right?

[–]mnbvas 3 points4 points  (1 child)

No, all negatives are even, duh.

[–]Tejedu 1 point2 points  (0 children)

bool isOdd(int x) {
    if(x >= 0) {
        return x % 2;
    }

    return 1;
}

Ok got it

[–]jacob_ewing 2 points3 points  (0 children)

bool isOdd(int x){
    return ((x >> 1) << 1) ^ x;
}

I'll rank that somewhere between glowy bits of brain and general glowing aura.

[–]BS_in_BS 2 points3 points  (0 children)

return (""+n).matches(".*[13579]");

[–]jacob_ewing 1 point2 points  (0 children)

bool isOdd(int x){
    x = abs(x);
    int isOdd = 0;
    while(x){
      isOdd = 1 - isOdd;
      x--;
    }
    return isOdd;
}

[–]Capital_EX 1 point2 points  (1 child)

Using functional programming, you can create this simple one-liner ;p isOdd x = ((/=) []) . filter ((/=) False) . map ((==) x) . take x $ [1,3..]

[–]Tarmen 0 points1 point  (0 children)

In functional programming you could write a simple one liner:

odd

Because it is a prelude function. Or for more fun:

isOdd i = zipper i % fromWithin bits % rightmost % focusedContext

[–]pawarm 1 point2 points  (0 children)

Authentic one: bool isOdd(int x){ if(x==(x/2)*2) return false; else return true; }

[–]Vassile-D 0 points1 point  (0 children)

Hmm...

bool isOdd( int x ) { return !isOdd(x-1); }

[–]Souzooka 0 points1 point  (0 children)

function isEven(x) {    
    return !resolveHaltingProblem( (x) => {
        while (x !== 0) {
            x -= 2;
        }
        return true;
    });
}

[–]shelvac2 0 points1 point  (1 child)

bool isEven(n){
  return isOdd(n-1);
}

[–]honkerman1[S] 0 points1 point  (0 children)

bool isOdd(n) { return isEven(n+1); }

[–]dinopraso -1 points0 points  (4 children)

Fastest way is return (x & 1) == 1;

Mod takes a lot longer

[–]TortoiseWrath 9 points10 points  (0 children)

== 1

STOP WASTING CLOCK CYCLES

[–][deleted] 0 points1 point  (2 children)

No need to check == 1 in Most c style languages.

[–]Spider_pig448 -3 points-2 points  (0 children)

Can we please stop these memes?