Skip to content

Instantly share code, notes, and snippets.

@luisfergromo
Forked from khajavi/floating_point_ieee.c
Created February 4, 2017 05:44
Show Gist options
  • Select an option

  • Save luisfergromo/de958018cce6702bcfbb5c56dd208af2 to your computer and use it in GitHub Desktop.

Select an option

Save luisfergromo/de958018cce6702bcfbb5c56dd208af2 to your computer and use it in GitHub Desktop.
Converting floating-point number to IEEE754 representation by using union and struct in c.
#include <stdio.h>
/*
* See also : http://class.ece.iastate.edu/arun/CprE281_F05/ieee754/ie5.html
*/
union FloatingPointIEEE754 {
struct {
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} raw;
float f;
} number;
int main() {
number.f = -6.8;
printf("%x, %x, %x", number.raw.mantissa, number.raw.exponent, number.raw.sign );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment