473,466 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trouble casting a struct with bit fields

Surely this is a no-brainer, but sometimes I think I must have no
brain (no comments, please<g>). Anyway, I have the following struct
containing bit fields:

struct ExtPenStyle
{
unsigned long style : 4;
unsigned long : 4; // unused
unsigned long endcap : 2;
unsigned long : 2; // unused
unsigned long join : 2;
unsigned long : 2; // unused
unsigned long type : 1;
unsigned long : 15; // unused
};

I tried casting from/to unsigned long using static_cast,
reinterpret_cast and C-style cast. The compiler refuses to do it, but
I need to call API code which expects an unsigned long with bitmapped
values.

I know I could use a union or memcpy(), but I wonder if there is a
cleaner way? Thanks.

#include <iostream>
#include <ostream>

// struct ExtPenStyle
// { see above };

int main()
{
using std::cout;
using std::endl;

ExtPenStyle xps = ExtPenStyle();
unsigned long ulong(static_cast<unsigned long>(xps));// no good?!?
cout << "Sizeof ExtPenStyle: " << sizeof(ExtPenStyle) << endl;
return 0;
}

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #1
3 4513
Sam
ExtPenStyle xps = ExtPenStyle();
unsigned long ulong(static_cast<unsigned long>(xps));// no good?!? Try this:
unsigned long ulong(*reinterpret_cast<unsigned long*>(&xps));
cout << "Sizeof ExtPenStyle: " << sizeof(ExtPenStyle) << endl;
return 0;
}

--
Bob Hairgrove
No**********@Home.com

Jul 22 '05 #2
On Wed, 17 Nov 2004 10:55:40 -0000, "Sam" <ma*******@hotmail.com>
wrote:
Try this:
unsigned long ulong(*reinterpret_cast<unsigned long*>(&xps));


Yes, this seems to work. However, I think I will use a union -- it
certainly saves a lot of typing, and seems more intuitive.

Thank you!
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #3
Bob Hairgrove <in*****@bigfoot.com> wrote in message news:<oi********************************@4ax.com>. ..
Surely this is a no-brainer, but sometimes I think I must have no
brain (no comments, please<g>). Anyway, I have the following struct
containing bit fields:

struct ExtPenStyle
{
unsigned long style : 4;
unsigned long : 4; // unused
unsigned long endcap : 2;
unsigned long : 2; // unused
unsigned long join : 2;
unsigned long : 2; // unused
unsigned long type : 1;
unsigned long : 15; // unused
};

I tried casting from/to unsigned long using static_cast,
reinterpret_cast and C-style cast. The compiler refuses to do it, but
I need to call API code which expects an unsigned long with bitmapped
values.


The compile is right, and the API couldn't care whether you
actually used bitfiels.

E.g.

template< int pos, int length >
struct field {
static unsigned long mask = (1UL<<length-1);
long get( unsigned long src ) { return (src>>pos) & mask }
void set( unsigned long& dest, long val ) {
val &= mask;
dest &= ~(mask<<pos);
dest |= val << pos;
}
};
struct ExtPenStyle
{
unsigned long bits;

field<0, 4> style_pos;
unsigned long get_style_pos() { return style_pos.get(bits); }
void set_style_pos(unsigned long v) { style_pos.set(bits, v); }

//etcetera
};

No casts needed. The compiler will do pretty much the same,
but this is more reliable (there are at least two ways to stuff
bitfields in a long, MSB or LSB first. Here YOU choose.)

HTH,
Michiel Salters
Jul 22 '05 #4

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

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
12
by: Ney André de Mello Zunino | last post by:
Hello. I have been having some trouble dealing with bit fields. The following is a simple program that demonstrates it. #include <iomanip> #include <iostream> struct instrucao_i {
3
by: Rob Jackson | last post by:
HiI've got a struct, known by file A.c, which contains a pointer to struct B. Struct B is unknown by file A.c (it is declared in C.h), and contains a typedef enum, which is declared in a file B.h,...
8
by: Servé Lau | last post by:
Consider the following code: struct X { float f; }; struct Y { struct X x; }; void func(struct X *x) {} int main(void) { struct Y y; func(&y);
17
by: goldfita | last post by:
I saw some code that appeared to do something similar to this struct foo { char offset; int d; }; struct foo { int a; int b;
6
by: crook | last post by:
I have code below and it works properly but when I'm compiling it with "--pedantic" flag, GCC(3.4.2) shows such warning: "ISO C forbids casting nonscalar to the same type". How can I change this...
2
by: pallav | last post by:
I'm using an old sparse matrix C library in my C++ code and I'd like to know how to downcast a boost::shared_ptr to char *. The sparse matrix data structure is like this: struct...
4
by: Schkhan | last post by:
Hi, I am sturggling with a peace of code mainly its about casting a pointer to one type of structure to a pointer to another type of structure. the confusion begins with the following...
4
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T>...
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...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.