C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28630
Number of posts: 94612

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
architecture related problem Posted by varunsh on 2 Feb 2010 at 10:16 PM
function f(float z)
which returns an unsigned number having the same bit representation as its floating-point argument.????
any answer???
Report
Re: architecture related problem Posted by dantebbs on 3 Feb 2010 at 12:12 PM
: 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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.