Skip to content

Instantly share code, notes, and snippets.

@dptole
Last active August 29, 2015 14:09
Show Gist options
  • Save dptole/dd4a9a73d6cfb0068c37 to your computer and use it in GitHub Desktop.
Save dptole/dd4a9a73d6cfb0068c37 to your computer and use it in GitHub Desktop.
Takes a floating point number and show its binary representation.
#include <iostream>
#include <stdio.h>
using namespace std;
void printDouble(double d) {
unsigned long long i = *(unsigned long long *) &d;
for(int j = 63; j >= 0; j--) {
cout << ((i >> j) & 1);
if(j == 63) cout << " ";
else if(j == 52) cout << " ";
}
cout << endl;
}
int main() {
cout << "Give me a floating point number and I'll show you its binary representation." << endl;
double d = 0;
scanf("%lf", &d);
printDouble(d);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment