nifty little thing i made
Here is a nifty little thing I made.
All of us have probably made a base 10 to base 2 converter before. Well, I took a new approach to it.
The most common approach is to mod by 2 recursively until you finally get down to 1's and 0's and form a base 2 number.
I did it quite differently.
I originally did this using ANSI C (char pointers, etc.), and it will definitely work that way if you want to make a version like that, but I changed it to use the STL string class to make it more readable.
#define BIT_1_MASK 0x00000001
string ConvertToBaseTwo ( int k )
char *c = new char;
for ( int i = 0; i < (sizeof(int)*8); i++ )
*c = ((k >> i) & BIT_1_MASK) + '0';
binary.prepend( c, 0, 1 );
binary.remove ( 0, binary.find_first_of("1"));