Jump to content
  • Advertisement
Sign in to follow this  
jbarcz1

Bit field layout

This topic is 4232 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
Why does field4 have to be in the MSB? (And why don't similar restrictions apply to the other fields?)

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!