Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4072114
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:50:37+00:00 2026-05-20T16:50:37+00:00

I want convert the storage of a floating point number to an integer (the

  • 0

I want convert the storage of a floating point number to an integer (the ‘number’ value is not required to be equal).

If a floating point number (eg 10), is represented in binary (at least on my implementation) as:

01000001001000000000000000000000

Then it should (also on my implementation) represent the integer value 1092616192.

I am currently doing this through the following:

union UNFI {
    float a;
    int b;
};

UNFI n;
n.a = 10;
int bits = n.b; // foo!

for (int i=31; i>=0; --i) {
    bool bit = ((bits >> i) & 1);
    std::cout << bit;
}

Which is achieving what I would like, but it is, in its nature, undefined behavior. Therefore I would like to know what the ‘correct’ way of achieving this outcome is.

From the C99 standard:

With one exception, if the value of a member of a union object is used when the most recent store to the object was to a different member,the behavior is implementation-defined.

Is that not undefined behavior?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:50:37+00:00Added an answer on May 20, 2026 at 4:50 pm

    The correct way is to call memcpy. Most compilers will optimize this to an efficient single-word memory access just like the union, but on platforms with extra alignment requirements, they will do the right thing. And it won’t ever trigger a signalling NaN.

    float a = 10;
    
    char bits[sizeof a];
    memcpy(bits, &a, sizeof a);
    

    or

    int b;
    static_assert(sizeof b == sizeof a);
    memcpy(&b, &a, sizeof a);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert a number to hex and store the result in a
I want to convert a pointer *int to its real value int , in
I want to convert XML into binary data in Java? What is the fastest
I've got a large text, containing a number as a binary value. e.g '123'
I want to convert an iplimage to a cv::mat (not CvMat). With this code
I want convert string value into date in my project because I am fetching
I want convert number larger than 5 become 1-5 again, ex: 6 become 1
I have an number I want convert its format to xxx xxx xxx,xx (will
I want to convert a string to formatted text in C# in WPF application,
I want to convert all the URLs in a javascript string to links, in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.