#include /* Note: this program requires double to be 64 bit and char to be 8 bit * It does NOT check this (e.g. via a sizeof-call) */ int main(int argc, const char** argv) { // Fail if num of cmd line args isn't 2 (first arg is program name!) if (argc!=2) { printf("Please use: bitwise [number] \n"); return(1);} // Define a union so the double and an array of 8 chars (1 byte each) // map to the same memory space union { double number; char bytes[8]; } value; sscanf(argv[1],"%lf", &value.number); // Conv. the first arg. to double int i; for (i=63;i>-1;i--) // Loop over all bits { if ( (value.bytes[i/8]) & ((char)1<<(i%8))) // shift and mask (logic and) printf("1"); else printf("0"); // Output if bit is 1 or 0 if (i%8==0) printf(" "); // Output a space to } // divide byte blocks printf("\n %.10f \n", value.number ); return(0); // print number, return }