Search code examples
cbinaryfloating-pointbitieee

Floating Point representation in Binary


I'm trying to print the a number stored in floating point in C and I'm struggling how to print it out. I'm doing this right now which prints the number in IEEE format. I want it printed in reverse straight from memory. How can I fix it? Thanks

void printbits(size_t n, void *c) {
    unsigned char *t = c; 
    if (c == NULL) 
        return; 
    while (n > 0) { 
        int q; 
        --n;
        for(q = 0x80; q; q >>= 1) 
            printf("%x", !!(t[n] & q));
        } 
}

Solution

  • Use a union:

    union u {
        float f;
        unsigned char p[4];
    }
    

    Then u.f = myfloat; and iterate on u.p