Skip to content

Instantly share code, notes, and snippets.

@florean
Created August 2, 2018 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save florean/3f0e7fd9ed3c92819f3fc3c89de9d7ee to your computer and use it in GitHub Desktop.
Save florean/3f0e7fd9ed3c92819f3fc3c89de9d7ee to your computer and use it in GitHub Desktop.
A simple FizzBuzz that uses counters instead of division.
#include <stdio.h>
int main() {
unsigned int fb = 0x0F0305; // 15 for fizzbuzz, 3 for fizz, and 5 for buzz
for (unsigned int i = 1; i < 101; i++) {
fb -= 0x010101; // Decrement one from each counter.
if (fb < 0X010000) {
fb = 0x0F0305; // Reset all counters to original value;
printf("fizzbuzz\n");
} else if ((fb & 0xFFFF) < 0x0100) {
fb += 0x0300; // Reset the fizz counter to 3.
printf("fizz\n");
} else if (!(fb & 0xFF)) {
fb += 5; // Reset the buzz counter to 5.
printf("buzz\n");
} else { printf("%d\n", i); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment