Created
October 25, 2011 21:34
-
-
Save cslarsen/1314379 to your computer and use it in GitHub Desktop.
FizzBuzz, can you spot any bit patterns?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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