architecture related problem

function f(float z)
which returns an unsigned number having the same bit representation as its floating-point argument.????
any answer???

Comments

  • : function f(float z)
    : which returns an unsigned number having the same bit representation
    : as its floating-point argument.????
    : any answer???

    Ah, the dangerous beauty of C is that this sort of thing is easy.

    For example:

    float fAlpha = 3.14159;
    unsigned long ulAlphaBits = *(unsigned long *)(&fAlpha);

    which on my current architecture places 0xD00F4940 into ulAlphaBits.

    This takes the pointer to the float of interest, casts it into a pointer to an int, and then dereferences it for the copy as an int.

    Use with caution though, since the answer will change if you go to
    a different architecture, so portability is low.

    -Dan
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion