public
Created

A small program to display binary representation of double numbers

  • Download Gist
display-double.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/*
* File: display-double.cpp
* Author: aspyct
*
* Can be compiled to a binaly that displays the binary value of double numbers.
* Usage : ./a.out <number1> ... <number n>
* Example: ./a.out 1.0 2.5
*
* Created on July 28, 2012, 11:03 PM
*/
 
#include <cstdlib>
#include <stdint.h>
#include <iostream>
 
using namespace std;
 
union number {
double d;
uint64_t i;
};
 
void display(double d);
void display(union number n);
 
int main(int argc, char** argv) {
int i;
double d;
for (i = 1; i < argc; ++i) {
sscanf(argv[i], "%lf", &d);
display(d);
}
 
return 0;
}
 
void display(double d) {
union number n;
n.d = d;
display(n);
}
 
void display(union number n) {
int i;
uint64_t base = 1;
cout << (n.i & (base << 63)) << " ";
for (i = 62; i > 51; --i) {
cout << ((n.i & (base << i)) >> i);
}
cout << " ";
for (; i >= 0; --i) {
cout << ((n.i & (base << i)) >> i);
}
cout << endl;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.