- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Packed structures
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2002 09:08 PM
12-01-2002 09:08 PM
Packed structures
Consider the following packed structure:
struct bit_field
{
unsigned int field_a: 2;
unsigned int field_b: 3;
unsigned int field_c: 5;
} flags;
According to K&R book the language does not
define whether storage starts at the least significant or most
significant bit. It is strictly up to the individual compiler.
Then how do I know how the compiler I'm using behaves? Is there a programming trick to get this information out of my compiler?
Thanks to all in advance...
Manuel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2002 09:45 AM
12-02-2002 09:45 AM
Re: Packed structures
The problem could be bigger as the compiler is allowed even to insert padds between your fields (for example it is allowed to force all fields not to cross byte - boundary).
In your case it could add something before field_c to make it start at the next byte. So your structure could be:
struct bit_field
{
unsigned int field_a: 2;
unsigned int field_b: 3;
unsigned int compiler_added_invisible_pad: (8 - ( 3 + 2 ))
unsigned int field_c: 5;
} flags;
But if you're just curios how it is organised wirte the programm to check it.
int main( void ) {
union {
long l;
struct bit_field bf;
} u;
u.l = 0;
u.bf.field_a = (unsigned)-1;
printf( "field_a bitmask: %lx\n", u.l );
u.l = 0;
u.bf.field_b = (unsigned)-1;
printf( "field_b bitmask: %lx\n", u.l );
u.l = 0;
u.bf.field_c = (unsigned)-1;
printf( "field_c bitmask: %lx\n", u.l );
return 0;
}
However BEWARE!
If you make yse of this knowledge inside your code and decide to make changes... (upgrade compiler, change compiling options) you're on your own and nobody will help you.
Good luck
Adam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2002 08:05 AM
12-03-2002 08:05 AM
Re: Packed structures
However I was wondering which the definition of struct bit_field would be?
I was not able to find it in the code snippet you posted...
Again thanks a lot for your help...
Manuel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2002 08:11 AM
12-03-2002 08:11 AM
Re: Packed structures
Of course I've already created that structure in my question...
Silly of me... ;-)
I apologize...
I was wondering what those
casts of -1 to (unsigned) would do?
Again thanks a lot to Adam and to anyone who takes the time to help me...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2002 10:40 AM
12-03-2002 10:40 AM
Re: Packed structures
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2002 11:08 AM
12-03-2002 11:08 AM
Re: Packed structures
Just, like Wodish said.
'-1' is very comfortable method of forcing all bits being set to 1.
Why did I use signed version and then used a cast? Because I'm lazy and I don't like bugs. :)
If you want char with all bits set to 1 you could count it carefully and write:
char all_1_c = 255;
However you could write also
char all_1_c = -1;
If you wanted short, you could do:
short all_1_s = 65535;
and also
short all_1_s = -1;
the same with long, and even more practical with non-common sizes as used in bitfields.
With the first solution if you decide to change the size after some time, from char to short you'll have to remember to recalculate the initialization:
char all_1 = 255; // I want it longer!
short all_1 = 255; // Oh, no! I forgot the second byte!
And belive me or not - if you have to remember plenty of such things you'll be lost at the second change of your code, so the best way is to make code when you have nothing to remember.
Then I made a cast, because your compiler could write a warning that I initialise unigned with negative. And belive me again, if you have warnings you'll get used to them and stop reading them. And sometimes they can save you a lot of work if you check them...
At the end - when I think about this I think that more readable form would be ~0 (with 'tylde' at the beginning), which means 'binary negated zero', which means 'all ones'. But '-1' is so commonly used that nobody cares... :)
Good luck
Adam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2002 11:15 AM
12-03-2002 11:15 AM
Re: Packed structures
I assume that this comes from the fact that we're using 2s complement and -1 in a system where `int' will have four bytes
-1 will be represented as
0xffff. Just let me know if I'm correct.
Thanks again...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2002 04:33 AM
12-04-2002 04:33 AM
Re: Packed structures
-1 in four-bytes representation is actually 0xffffffff (as one charcter in this representation stands for four bits).
Consequently 0xffff stands for two bytes.
Rest of your assumption is absolutelly correct.
Results from the code I've sent you is bit-mask, that is all bits for the field are set to '1', other are '0'. The results are written as hexadecimal number. The most comfortable would be to write it binary, but printf does not have an option to write binary. Only signed decimal, unsigned decimal, hexadecimal and octal.
So you'll recive something like (this can of course change depending on what you're about to check):
field_a bitmask: c0000000
field_a bitmask: 38000000
field_a bitmask: 7c00000
which you'll have to investigate further.
For your convenience:
h 1 = b 0001
h 2 = b 0010
h 4 = b 0100
h 8 = b 1000
h 3 = b 0011
h 6 = b 0110
h c = b 1100
h 7 = b 0111
h e = b 1110
h f = b 1111
Good luck
Adam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2002 06:59 AM
12-04-2002 06:59 AM
Re: Packed structures
Thanks a lot again for your help...
Your good and detailed explanation has made things a whole lot easier to understand...
I appreciate it...
Manuel