473,395 Members | 1,969 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,395 software developers and data experts.

Union

I have some data packed in by bitfields:

struct Data
{
unsigned long a : 8;
unsigned long b : 14;
unsigned long c : 7;
unsigned long d : 3;
};

I want to be able to easily address and move etc. the underlying
unsigned long that these are packed into. Something like

union U
{
Data data;
unsinged long ul;
};

U u;

u.a = ...;
u.b = ....;

unsigned long x = u.ul;

Is there an easier way to achieve this?

/David
Jul 22 '05 #1
2 1905
David Rasmussen posted:
I have some data packed in by bitfields:

struct Data
{
unsigned long a : 8;
unsigned long b : 14;
unsigned long c : 7;
unsigned long d : 3;
};

I want to be able to easily address and move etc. the underlying
unsigned long that these are packed into. Something like

union U
{
Data data;
unsinged long ul;
};

U u;

u.a = ...;
u.b = ....;

unsigned long x = u.ul;

Is there an easier way to achieve this?

/David

struct Data
{
unsigned long a : 8;
unsigned long b : 14;
unsigned long c : 7;
unsigned long d : 3;
};

I've never seen anything like that before! What do the colon and the
number after it do?!

--

Anyways, you'll have to write:

U.data.a = 1;
U.data.b = 2;

--

What exactly is it you are trying to do?!
Jul 22 '05 #2
"David Rasmussen" <da*************@gmx.net> wrote...
I have some data packed in by bitfields:

struct Data
{
unsigned long a : 8;
unsigned long b : 14;
unsigned long c : 7;
unsigned long d : 3;
};

I want to be able to easily address and move etc. the underlying
unsigned long that these are packed into. Something like

union U
{
Data data;
unsinged long ul;
};

U u;

u.a = ...;
u.b = ....;

unsigned long x = u.ul;

Is there an easier way to achieve this?


Add a conversion operator to the structure.

struct Data
{
...
operator unsigned long() const
{
// do what you need to do
}
};

...

Data data;
data.a = ...
data.b = ...
unsigned long x = data;

Of course the "what you need to do" can be as simple as forming
a union and returning the value of the 'ul', or it could be
something different (in case you need it). The simple thing:

...
operator unsigned long() const
{
union { Data d; unsigned long ul; } u = { *this };
return u.ul;
}

Although, and we discussed it in comp.std.c++ at some point, C++
does NOT guarantee the validity of such operation. The union exists
not to convert the same data representations from one type to another
but to save memory. The Standard is written so that you can only
extract from a union what you put in it. In your case, if you put
(assign or initialise) a Data in the union, you are not guaranteed
to get the "underlying unsigned long" from the other member. I could
recommend using reinterpret_cast:

Data data;
data.a = ...
data.b = ...
unsigned long ul = *reinterpret_cast<unsigned long*>(&data);

but that's not guaranteed by the Standard either (though some would
argue that "it does what we need").

The only guaranteed way is to write your conversion operator this way:

operator unsigned long() const
{
return a + (b << 8) + (c << 22) + (d << 29);
}

and then you can get the correct value (if that's the layout you
designed it to be).

Victor
Jul 22 '05 #3

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

Similar topics

5
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
2
by: Barry Schwarz | last post by:
Given a union of the form union { T1 m1; T2 m2;}obj; where T1 and T2 are different scalar (non-aggregate) types. The C99 standard states that obj.m1 = value; if (obj.m2 ... invokes...
10
by: Denis Pithon | last post by:
Hi, C lovers! I stuck on an union problem Here is snippet of my code .... /* two pointers of function with repsectively one and two argues */ typedef int (*dce_sn_f)(dce_t*);
2
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the...
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
18
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
3
by: SRK | last post by:
Hi, I wanted to use an anonymous union within an structure something like below - struct Test { union { std::string user; //char user; std::string role; //char role;
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.