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

  • Home
  • SEARCH
  • 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 4115512
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:35:29+00:00 2026-05-20T22:35:29+00:00

I’m having some difficulty understanding why the output of the following code is not

  • 0

I’m having some difficulty understanding why the output of the following code is not what I expect. In the first loop iteration all values print correctly, however in all subsequent iterations the output of fi_union.float_val is 0.

I’m using the union to essentially replace pointer aliasing, and be able to interpret the 64 bits as an uint64_t or an IEEE 754 double precision value. Is this the proper way to go about this?

I know bit shifting a IEEE 754 value will result in garbage essentially. As odd as it sounds, that’s what I’m going for. I really would like to know why fi_union.float_val is zero after the first iteration. I don’t have any GCC optimizations turned on.

I’m using gcc version 4.4.3 on an x86_64 architecture.

double v1 = 0xDE.62133p0;

union float_interpret
{
    uint64_t val;
    double float_val;

} fi_union;


for( int i = 0; i <= 35; i++ )
{
    fi_union.val = *(uint64_t*)(&v1) >> i;
    printf( "\n %f >> %*i = %16lX \t %20li \t %15f", v1, 2, i, fi_union.val, fi_union.val, fi_union.float_val );
}
  • 1 1 Answer
  • 0 Views
  • 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-20T22:35:30+00:00Added an answer on May 20, 2026 at 10:35 pm

    Floating-point variables, according to IEEE 754, are composite data structures with three or four components: sign, exponent, mantissa and optionally a quiet NaN flag (embedded in the mantissa). Shifting the whole memory representation like you did in fi_union.val = *(uint64_t*)(&v1) >> i; will not return anything meaningful.

    So the expected results are indeed what you see, on the first iteration, when i = 0, no shifting is performed and the value returned is ok. When you start shifting bits in the memory representation of the float, you are trowing the sign bit over the exponent, the exponent bits over the mantissa, etc. This causes a mess.

    If you want to inspect the contents of the memory representation of the floating-point number, use <ieee754.h> from glibc.

    Something like this (not tested):

    #include <ieee754.h>
    ...
    union ieee754_double x;
    
    x.d = v1
    printf("sign: %u; mantissa: %llu; exponent: %u\n",
        (unsigned) x.ieee.negative,
        ((unsigned long long) x.ieee.mantissa1 << 20) | ((unsigned long long) x.ieee.mantissa0),
        (unsigned) x.ieee.exponent - IEEE754_DOUBLE_BIAS);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.