Skip to content

Instantly share code, notes, and snippets.

@LizardLeliel
Created November 14, 2014 17:53
Show Gist options
  • Save LizardLeliel/89ca071e8edfccf630b6 to your computer and use it in GitHub Desktop.
Save LizardLeliel/89ca071e8edfccf630b6 to your computer and use it in GitHub Desktop.
It actually prints out the binaries of double numbers
#include <cstdlib>
#include <cstdio>
#include <iostream>
// Last one is for cin.ignore()
using namespace std;
typedef enum binDigPlaces {
b1 = 0x01,
b2 = 0x02,
b3 = 0x04,
b4 = 0x08,
b5 = 0x10,
b6 = 0x20,
b7 = 0x40,
b8 = 0x80,
} binPlaces;
void printByte(char m);
void printDouble(double n);
int main(void) {
printf("%d\n", sizeof(char));
printf("%d\n", b1 | b2 | b3 | b4 | b5 | b6 | b7 | b8);
printByte('f'); printf("\n");
double number = 5787.355352;
printf("%f\n\n", number);
char* pointer = (char*)&number;
printDouble(number);
double floating = 0.0;
for (int counter = 0; counter < 10; floating = floating + 2.0, ++counter) {
pointer = (char*)&floating;
printf("%f\n", floating);
for (int i = 0; i < sizeof(double); ++i, ++pointer) {
printByte(*pointer); printf("\n");
} printf("\n");
}
floating = 1.0;
printf("1.0");
printDouble(floating);
for (int i = 0; i < 10; floating *= 10, ++i) {
printf("%f\n", floating);
printDouble(floating);
}
cin.ignore();
}
void printDouble(double n) {
char* pointer = (char*)&n;
for (int i = 0; i < sizeof(double); ++i, ++pointer) {
printByte(*pointer); printf("\n");
} printf("\n");
}
void printByte(char m) {
int range[8] = { b1, b2, b3, b4, b5, b6, b7, b8 };
for (int i = 0; i < 8; ++i) {
if (m & range[i]) {
printf("1");
}
else {
printf("0");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment