Skip to content

Instantly share code, notes, and snippets.

@cslarsen
Created October 25, 2011 21:34
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 cslarsen/1314379 to your computer and use it in GitHub Desktop.
Save cslarsen/1314379 to your computer and use it in GitHub Desktop.
FizzBuzz, can you spot any bit patterns?
/*
* FizzBuzz
*/
#include <stdio.h>
char* bits(int n)
{
#define BITS 8
#define BIT(x) (x? '1' : ' ');
static char s[BITS+1] = "";
s[0] = BIT((n & 0x100));
s[1] = BIT((n & 0x80));
s[2] = BIT((n & 0x40));
s[3] = BIT((n & 0x20));
s[4] = BIT((n & 0x10));
s[5] = BIT((n & 0x8));
s[6] = BIT((n & 0x4));
s[7] = BIT((n & 0x2));
s[8] = BIT((n & 0x1));
return s;
}
int main()
{
int n;
for ( n=1; n<=100; ++n ) {
printf("%3d %s %s%s\n", n, bits(n),
(n % 3)==0 ? "Fizz" : "",
(n % 5)==0 ? "Buzz" : "");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment