Wednesday, March 20, 2013

bitChal3 -- Convert double to a binary value

#include <iostream>

using namespace std;

void bitDump(void* address, unsigned int size);

int main(){
  long double ld = 12345.123456;
  bitDump(&ld, sizeof(ld)); // print the bit pattern (cool if formatted)
  return 0;
}
//Loop through size and convert each char to 8 binary digits.
void bitDump(void* address, unsigned int size){
    char* c = (char*) address;
    int cnt = 1;
    for(int i = 0; i < size; i++){
        unsigned int m = 1 << 7;
        while(m){//convert each char to 8 binary digits
            printf("%d", !!(c[i]&m));
            m >>= 1;
            cnt == 4 ? putchar(' '), cnt = 1 : cnt++;
        }
    }
    putchar('\n');
}

No comments:

Post a Comment