Hacker News new | ask | show | jobs
by boring_twenties 1562 days ago
FWIW, gcc 10 generates identical code for `i % 2 != 0` and `i & 1 != 0` at -O1.
1 comments

Yeah, but "doesn't know about the % operator" is a lot less damning than "can only think of an exponential-time algorithm to determine if a number is even". Like, this is terrible, but it's linear time:

    char s[32];
    sprintf(s, "%d", n);
    char c = s[strlen(s) - 1];
    return c == '0' || c == '2' || c == '4' || c == '6' || c == '8';