Skip to content

Instantly share code, notes, and snippets.

@Un1Gfn
Created February 11, 2020 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Un1Gfn/5a46248786f27a1c7c60d9f143023c5d to your computer and use it in GitHub Desktop.
Save Un1Gfn/5a46248786f27a1c7c60d9f143023c5d to your computer and use it in GitHub Desktop.
// gcc -std=c11 -g -O0 -Wall -Wextra -fdiagnostics-color=always ieee754.c
/*
$ ./a.out x 0 x inf -inf x nan -nan x 1 1.5 1.75 1.875 -1.875 x
0 00000000 00000000000000000000000
0 11111111 00000000000000000000000
1 11111111 00000000000000000000000
0 11111111 10000000000000000000000
1 11111111 10000000000000000000000
0 01111111 00000000000000000000000
0 01111111 10000000000000000000000
0 01111111 11000000000000000000000
0 01111111 11100000000000000000000
1 01111111 11100000000000000000000
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// for(var:range)
#define for_range_int(var,range) for(size_t var=0;var<sizeof(range)/sizeof(int);++var)
#define reinterpret(new_type,var) (*((new_type*)(&var)))
int main(int argc,char const *argv[]){
for(int c=1;c<argc;++c){
if(strcmp("x",argv[c])==0){
printf("\n");
continue;
}
float f=atof(argv[c]);
int partition[]={1,8,23};
for_range_int(part,partition){
for(int i=0;i<partition[part];++i){
printf( (reinterpret(uint32_t,f)&0x80000000) ? "1" : "0" );
reinterpret(uint32_t,f)<<=1;
}
printf(" ");
}
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment