Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
9
Task/Gray-code/C/gray-code-1.c
Normal file
9
Task/Gray-code/C/gray-code-1.c
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
int gray_encode(int n) {
|
||||
return n ^ (n >> 1);
|
||||
}
|
||||
|
||||
int gray_decode(int n) {
|
||||
int p = n;
|
||||
while (n >>= 1) p ^= n;
|
||||
return p;
|
||||
}
|
||||
24
Task/Gray-code/C/gray-code-2.c
Normal file
24
Task/Gray-code/C/gray-code-2.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include <stdio.h>
|
||||
|
||||
/* Simple bool formatter, only good on range 0..31 */
|
||||
void fmtbool(int n, char *buf) {
|
||||
char *b = buf + 5;
|
||||
*b=0;
|
||||
do {
|
||||
*--b = '0' + (n & 1);
|
||||
n >>= 1;
|
||||
} while (b != buf);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int i,g,b;
|
||||
char bi[6],bg[6],bb[6];
|
||||
|
||||
for (i=0 ; i<32 ; i++) {
|
||||
g = gray_encode(i);
|
||||
b = gray_decode(g);
|
||||
fmtbool(i,bi); fmtbool(g,bg); fmtbool(b,bb);
|
||||
printf("%2d : %5s => %5s => %5s : %2d\n", i, bi, bg, bb, b);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue