473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

unions with long long ints and doubles?

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 ch*********@arm.com
(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.
Nov 13 '05 #1
3 4015
"Chris N. Hinds" <ch*********@arm.com> wrote in message
news:bl**********@cam-news1.cambridge.arm.com...
| 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
Nov 13 '05 #2
In message <bl**********@cam-news1.cambridge.arm.com>
"Chris N. Hinds" <ch*********@arm.com> 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/
Nov 13 '05 #3
On Tue, 30 Sep 2003 09:09:39 -0500, "Chris N. Hinds"
<ch*********@arm.com> 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>>
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Koen | last post by:
Hi! Does anyone know what the standard says about the way unions are stored in C? I mean the following: Let's say you have a union with a double and a char field: union MyUnion_t { double...
6
by: Chuck Bowling | last post by:
I have a struct that i want to emulate a C++ style union: public struct Samp { public byte byteBuf; public int intBuf; public Samp(int sz) {
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
35
by: aNt17017 | last post by:
This is my code: long fact(int n) { if (n == 0) return(1); if(n > 100) { printf("\t\tERROR: %d is too large for factorial.\n", n); return 1;
67
by: bluejack | last post by:
A recent post asking for help with unions reminded me of this component of the C language that I have only used a couple of times, and those almost entirely out of personal whim -- Unions for the...
26
by: Old Wolf | last post by:
Ok, we've had two long and haphazard threads about unions recently, and I still don't feel any closer to certainty about what is permitted and what isn't. The other thread topics were "Real Life...
3
by: nineoo | last post by:
To all, Ok , now this may sound a little crazy, but does any one know if there is a way to increase the maximum value of a unsigned long int? I'm aware of the the max value of an...
48
by: Bill Cunningham | last post by:
Is this valid C syntax ? double=double/int; I seem to be having trouble here. Bill
2
by: KioKrofov | last post by:
I am writing code in C to print out various data values from some test I am running. The data results come in ints, long ints, floats and doubles. Most data values are required to be in decimal,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.