Sign in to follow this  
jbarcz1

Bit field layout

Recommended Posts

jbarcz1    265
Hello, Does anyone know if there are any C standards with regard to the layout of bits in a bit field (most-significant to least significant or vice versa?) I'm trying to define a bit field like this one, and I need to make sure that the five-bit field goes into the most-significant bits of the DWORD
struct Foo
{
   unsigned field1 : 9 ;
   unsigned field2 : 9 ;
   unsigned field3 : 9 ;
   unsigned field4 : 5 ;  // This must go into the MSB
}
The VCPP documentation claims that microsoft's compiler allocates bit fields the way I want, but this is marked 'microsoft specific' and I'd like to know if there is a defined standard.

Share this post


Link to post
Share on other sites
jpetrie    13106
Allocation and alignment of bit fields are implementation-defined, and may even bridge allocation units on some machines. They might be assigned left-to-right or right-to-left; so in other words, no, you cannot ensure field4 ends up using the five most-signifigant bits across implementations.

Share this post


Link to post
Share on other sites
scottdewald    163
Even though you say C, you mention VCPP, so I assume you can you C++. Looks like you need to manage multiple fields that span 32 bits. Why don't you just create a class that has a data member of type uint32_t, and then make accessor functions to get/set the fields you need?

Share this post


Link to post
Share on other sites
jbarcz1    265
Quote:
Original post by scottdewald
Even though you say C, you mention VCPP, so I assume you can you C++. Looks like you need to manage multiple fields that span 32 bits. Why don't you just create a class that has a data member of type uint32_t, and then make accessor functions to get/set the fields you need?


Thats probably what I'll end up doing, I was just looking for a way to avoid coding the bit twiddling myself.

JB

Share this post


Link to post
Share on other sites
jbarcz1    265

I actually need all of those fields ordered from LSB to MSB. This struct actually represents a pixel in a D3D10 shared-exponent texture, and so I need it to match the D3D10 bit layout.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this