Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Here is a version with 0 if statements

    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char** argv) {
        return printf(atoi(argv[1]) & 1 ? "odd" : "even") < 0;
    }


Still branches


  #include <stdio.h>
  #include <stdlib.h>
  int main(int argc, char** argv) {
      return printf(&"even\0odd"[(atoi(argv[1]) & 1) * 5]) < 0;
  }


Not always. Popular architectures like x86_64 and ARM have instructions for conditional moves. These instructions do not cause the processor to branch.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: