Skip to content

Instantly share code, notes, and snippets.

@andrewgho
Created August 4, 2015 06:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewgho/6a37d71d9848896af12a to your computer and use it in GitHub Desktop.
Save andrewgho/6a37d71d9848896af12a to your computer and use it in GitHub Desktop.
Branchless FizzBuzz
#include <stdio.h>
int main() {
int i, idx, mask[] = {3, 0, 0, 0, 0};
char buf[4], *out[] = {buf, "Fizz", "Buzz", "FizzBuzz"};
for(i = 1; i <= 100; i++) {
sprintf(buf, "%d", i);
idx = (mask[i % 3] & 1) + (mask[i % 5] & 2);
printf("%s\n", out[idx]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment