Created
August 4, 2009 02:37
-
-
Save pi8027/160991 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <stdio.h> | |
#define binary_to_gray_code(binary) ((binary)^(binary)>>1) | |
int fputb(unsigned int input,unsigned int digit,FILE *output) | |
{ | |
if(digit){ | |
fputb(input>>1,digit-1,output); | |
fputc('0'+(input&1),stdout); | |
} | |
return input; | |
} | |
unsigned int true_bit_counter(unsigned int input) | |
{ | |
if(!input){ | |
return 0; | |
} | |
else{ | |
return (input&1)+true_bit_counter(input>>1); | |
} | |
} | |
unsigned int gray_code_increment(unsigned int input) | |
{ | |
if(!(true_bit_counter(input)&1)){ | |
return input^1; | |
} | |
else{ | |
return gray_code_increment(input>>1)<<1|(input&1); | |
} | |
} | |
int main(void) | |
{ | |
unsigned int gray_code = binary_to_gray_code(0); | |
while(gray_code != binary_to_gray_code(64)){ | |
fputb(gray_code,6,stdout); | |
fputc('\n',stdout); | |
gray_code = gray_code_increment(gray_code); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment