Skip to content

Instantly share code, notes, and snippets.

@leyafo
Last active March 24, 2021 09:22
Show Gist options
  • Save leyafo/b3a208d13767528e6c91d8d7386cfcda to your computer and use it in GitHub Desktop.
Save leyafo/b3a208d13767528e6c91d8d7386cfcda to your computer and use it in GitHub Desktop.
IEEE754 presenting
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
void printIEEE754(size_t const size, void const * const ptr){
unsigned char *b = (unsigned char*) ptr;
unsigned char byte;
int i, j, c=0;
if(size == 4){
printf("%f\n", *(float*)ptr);
}else if (size == 8){
printf("%f\n", *(double*)ptr);
}
for (i = size-1; i >= 0; i--) {
for (j = 7; j >= 0; j--) {
byte = (b[i] >> j) & 1;
printf("%u", byte);
c++;
if (c==1){
printf(" ");
}
if (c==12 && size*8 == 64){
printf(" ");
}
if (c==9 && size*8 == 32){
printf(" ");
}
}
}
puts("\n");
}
int main(int argv, char* argc[])
{
float f;
f = 5.0;
printIEEE754(sizeof(f), &f);
f = -5.0808;
printIEEE754(sizeof(f), &f);
f = 0.085f;
printIEEE754(sizeof(f), &f);
f = 3.14f;
printIEEE754(sizeof(f), &f);
double d;
d = 3.1415927;
printIEEE754(sizeof(d), &d);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment