unions with long long ints and doubles?

Discussion in 'C Programming' started by Chris N. Hinds, Sep 30, 2003.

  1. I have a question regarding accessing long long ints in unions. I have
    constructed a union with a double, two ints in a structure, and a long
    long int. When the double is loaded with a floating-point constant, the
    double is loaded correctly, and the two ints in the structure reflect
    the double correctly when printed as hex values. However, when the long
    long is printed as a hex value (using %016x) it prints 8 zeros then the
    upper 32-bits of the double.

    What am I doing wrong here? I want to be able to manipulate the double
    using the long long ultimately using bit selects (that didn't work
    either...).

    Thanks for any help!

    chris

    The code for the union and the print statements and output follow.

    The following union

    typedef union uDblLLInts {
    struct {
    int iDHi;
    int iDLo;
    };

    double dbl;

    unsigned long long int llD;
    } DblLLInts;

    when initialized with:

    DblLLInts DblA;
    DblA.dbl = 1.000005;

    prints this:
    dbl is 1.000005e+00
    int parts are 3ff000053e2d6239
    long long is 000000003ff00005

    for these printfs:

    printf(" dbl is %e\n", DblA.dbl);
    printf(" int parts are %08x%08x\n", DblA.iDHi, DblA.iDLo);
    printf(" long long is %016x\n", DblA.llD);

    --
    *******************************************************
    Chris N. Hinds <>< www.arm.com
    ARM Austin Design Center
    (512) 314-1055 (Direct)
    (512) 327-9249 (Front Desk)
    (512) 314-1078 (Fax)
    *******************************************************
    This e-mail message is intended for the addressee(s) only and may
    contain information that is the property of, and/or subject to a
    confidentiality agreement between the intended recipient(s), their
    organization and/or the ARM Group of Companies. If you are not
    an intended recipient of this e-mail message, you should not read,
    copy, forward or otherwise distribute or further disclose the
    information in it; misuse of the contents of this e-mail message
    may violate various laws in your state, country or jurisdiction.
    If you have received this e-mail message in error, please contact
    the originator of this e-mail message via e-mail and delete all
    copies of this message from your computer or network, thank you.
     
    Chris N. Hinds, Sep 30, 2003
    #1
    1. Advertising

  2. "Chris N. Hinds" <> wrote in message
    news:blc2r6$rck$...
    | I have a question regarding accessing long long ints in unions. I have
    | constructed a union with a double, two ints in a structure, and a long
    | long int. When the double is loaded with a floating-point constant, the
    | double is loaded correctly, and the two ints in the structure reflect
    | the double correctly when printed as hex values. However, when the long
    | long is printed as a hex value (using %016x) it prints 8 zeros then the
    | upper 32-bits of the double.
    ....
    | typedef union uDblLLInts {
    | struct {
    | int iDHi;
    | int iDLo;
    | };
    Note: anonymous struct within a union are not a standard C feature.
    (it's a compiler-specific extension).
    .....
    | prints this:
    .....
    | long long is 000000003ff00005
    ....
    | for these printfs:
    ....
    | printf(" long long is %016x\n", DblA.llD);
    Note: this printf call expects an unsigned int as a parameter.
    What you are triggering is undefined behavior (which happens
    on your platform to drop the high 32 bits of the value).

    Try:
    printf(" long long is %016llx\n", DblA.llD);

    (note the 'll' added to the conversion specifier)


    I hope this helps,
    Ivan
    --
    http://ivan.vecerina.com
     
    Ivan Vecerina, Sep 30, 2003
    #2
    1. Advertising

  3. Chris N. Hinds

    Kevin Bracey Guest

    In message <blc2r6$rck$>
    "Chris N. Hinds" <> wrote:

    > I have a question regarding accessing long long ints in unions. I have
    > constructed a union with a double, two ints in a structure, and a long
    > long int. When the double is loaded with a floating-point constant, the
    > double is loaded correctly, and the two ints in the structure reflect
    > the double correctly when printed as hex values. However, when the long
    > long is printed as a hex value (using %016x) it prints 8 zeros then the
    > upper 32-bits of the double.


    Your format specifier doesn't match the type - the Norcroft ARM compiler
    should have warned about that. You want "%016llx".

    --
    Kevin Bracey, Principal Software Engineer
    Tematic Ltd Tel: +44 (0) 1223 503464
    182-190 Newmarket Road Fax: +44 (0) 1223 503458
    Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
     
    Kevin Bracey, Sep 30, 2003
    #3
  4. On Tue, 30 Sep 2003 09:09:39 -0500, "Chris N. Hinds"
    <> wrote:

    >I have a question regarding accessing long long ints in unions. I have
    >constructed a union with a double, two ints in a structure, and a long
    >long int. When the double is loaded with a floating-point constant, the
    >double is loaded correctly, and the two ints in the structure reflect
    >the double correctly when printed as hex values. However, when the long
    >long is printed as a hex value (using %016x) it prints 8 zeros then the
    >upper 32-bits of the double.


    As of C99, accessing members of a union other than the one last stored
    generally results in undefined behavior. You example is not one of
    the exceptions to this.

    >
    >What am I doing wrong here? I want to be able to manipulate the double
    >using the long long ultimately using bit selects (that didn't work
    >either...).
    >
    >Thanks for any help!
    >
    >chris
    >
    >The code for the union and the print statements and output follow.
    >
    >The following union
    >
    > typedef union uDblLLInts {
    > struct {
    > int iDHi;
    > int iDLo;
    > };
    >
    > double dbl;
    >
    > unsigned long long int llD;
    > } DblLLInts;
    >
    >when initialized with:
    >
    > DblLLInts DblA;
    > DblA.dbl = 1.000005;
    >
    >prints this:
    > dbl is 1.000005e+00
    > int parts are 3ff000053e2d6239
    > long long is 000000003ff00005
    >
    >for these printfs:
    >
    > printf(" dbl is %e\n", DblA.dbl);
    > printf(" int parts are %08x%08x\n", DblA.iDHi, DblA.iDLo);
    > printf(" long long is %016x\n", DblA.llD);




    <<Remove the del for email>>
     
    Barry Schwarz, Oct 2, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dan
    Replies:
    1
    Views:
    2,363
    Jack Klein
    Nov 26, 2003
  2. Replies:
    3
    Views:
    609
    Mark P
    Apr 3, 2005
  3. Skybuck Flying

    ints ints ints and ints

    Skybuck Flying, Jul 8, 2004, in forum: C Programming
    Replies:
    24
    Views:
    871
    Jack Klein
    Jul 10, 2004
  4. Ray Dillinger

    format specifier for long long ints....

    Ray Dillinger, Mar 26, 2006, in forum: C Programming
    Replies:
    4
    Views:
    506
    Keith Thompson
    Mar 27, 2006
  5. Bill Cunningham

    doubles and ints

    Bill Cunningham, Jul 11, 2008, in forum: C Programming
    Replies:
    48
    Views:
    1,059
Loading...

Share This Page