Awesomely stupid gamma-code encoder

Coded while professor explained what is the gamma code. Horribly inefficient but apparently works.

#
include <memory.h> #include <math.h> #include <stdio.h> int binlog(int number) { return log((float)number) / log(2.0); } int main() { int value = 26; char buf[0x100]; int i; int position = 0; memset(buf, 0x100, 0); printf("%d\n", binlog(value)); int power = binlog(value); for ( i = 0; i < power; ++i ) { buf[position++] = '0'; } buf[position++] = '1'; int remains = value - ( 1 << power ); int tmppos = position; for ( i = 0; i < power-1; ++i ) { buf[tmppos++] = '0'; } i = 0; while ( remains != 0 ) { printf("Remains: %d\n", remains); buf[tmppos-i] = '0' + (remains & 1); remains >>= 1; ++i; } printf("%s\n", buf); return 0; }
Filed under  //   geek  

Comments (0)

Leave a comment...

 
To leave a comment on this posterous, please login by clicking one of the following.
Posterous-login     Connect     twitter

About

A mad-eye programmer. No, really!