Fun with IEEE 754

As you may know, standard IEEE 754 defines the format of floating point representation. I came across with this IEEE 754 Converter that shows you how a certain value will be represented on this format in binary and hex.

So, for example, let's test 3.1416:

So, I was wondering how we can get this hex representation in my machine;

Let's figure out!

First, I declare a float variable and print it:

#include <stdio.h>

main()
{
    float pi = (float) 3.1416;
    printf("%f\n", pi);
}

Nothing strange here. We have the output we expected.

The strategy here will be to "fool" the compiler to print this as an integer, but if we try the following:

#include <stdio.h>
main()
{
    float pi = (float)3.1416;
    printf("%f\n", pi);

    printf("%x\n", (int)pi);
}

The cast will truncate the decimals. That same would happen if we just do

printf("%x", pi);

The %x will cast to int before reformatting the output to hex.

So, what other alternative we have? Let's try a solution with pointers: 

#include <stdio.h>

main()
{
    float pi = (float)3.1416;
    float* f_ptr = &pi;

    printf("%f\n", pi);
    printf("%f\n", *f_ptr);
    printf("%x\n", *((int *)f_ptr));
}

aaaaaand bingo! That third output is the one we expected and correspond to the first image. But, what happened in the code?

The second output is just for reference and it just shows that derefencing the pointer will get us the value of "pi"

But the third output, let me explain myself:

printf("%x\n", *((int*)f_ptr));

Here what it is done in the second argument is that the pointer to float (float) is casted to int* . That's the beauty of C, it allows the programmer to tell the compiler "Ok, I know I'm giving you a float* but let's pretend this in this case a pointer to int" and so, once we dereference it (leftmost *, giving us an int) and convert the output to hex we get the expected output.


Great demonstration Marco!

Like
Reply

To view or add a comment, sign in

More articles by Marco Ramirez (MSc.) 🚕

  • On Code Reviews

    We've heard that, we know that. Humans are terrible in so many aspects.

  • On Agile

    February 11th was the 20th anniversary where the Agile manifesto was created. It is crazy to imagine how much the world…

  • On Architecture

    Buildings, Spaces, Interior. Those are the words that come to my mind when I hear that word, Architecture.

    3 Comments
  • Can bus turns 35!

    It was 35 years ago on February 25, 1986 that Bosch presented the CAN protocol at the SAE conference in Detroit. Since…

  • Low Level Programming University

    I recently found this page in Github and Hacker News . It contains an structured curricula for learning "close to the…

    1 Comment
  • Continental Expert Day 2017

    I had the honor to participate in the 1st North America Regional Expert Day held on Deer Park, IL on April 27th. The…

  • Weekend Project (K&R)

    I'm starting a weekend routine in which I set up a project on Friday for the weekend. A weekend coding challenge so to…

  • Software Engineering Mondays

    In this new section every Monday I will put some links to some interesting articles. Enjoy :D Tour of F# https://docs.

  • Workplace

    The hardest aspect of being a Software Developer is juggling with many things in our head. In an nutshell, you have to…

  • Barcamps have been great for bringing social to a development environment. Hope to continue doing these and continue learning.

Others also viewed

Explore content categories