J
Jon Slaughter
I'm using bit fields to compactly represent some data I need to manage and
I've read that they are not portable. I don't understand why that is the
case? As long as I don't "indirectly" mess with the fields and I go through
the compiler and allow it to choose the appropriate way to handle them then
why shouldn't it be ok?
The whole point of me using bit fields was so that I could be somewhat
portable instead of managing the bits myself(by extracting and inserting
them into ints using the logical operators).
I understand that the compiler doesn't guarantee alignment or packing but I
feel that as long as I let the compiler deal with the fields then it should
be ok?
Also, I read that there can be a "speed" hit when using bitfields? I don't
really see how this can be true since it only involves a few logical
operators and shifts? (Maybe in specific instances where one is looping over
something that runs in a few cycles where these extra instructions could be
significant might it be a big deal but I don't see why in general)
So, if there are at times a speed issues... for example, lets say that I run
my program and it turns out by using the "bit fields" I get a speed hit...
is there any way to force the compiler to turn those "bit fields" into
standard types(such as int)
i.e.
lets say I have
struct blah
{
unsigned a : 4;
unsigned b : 1;
unsigned d : 9;
}
and I want to "increase the speed", could I tell it to "implicitly" convert
the bit fields into ints? something like
struct blah
{
unsigned a : 8;
unsigned b : 8;
unsigned d : 16;
}
or even
struct blah
{
unsigned a : 32;
unsigned b : 32;
unsigned d : 32;
}
?
Thanks
Jon
I've read that they are not portable. I don't understand why that is the
case? As long as I don't "indirectly" mess with the fields and I go through
the compiler and allow it to choose the appropriate way to handle them then
why shouldn't it be ok?
The whole point of me using bit fields was so that I could be somewhat
portable instead of managing the bits myself(by extracting and inserting
them into ints using the logical operators).
I understand that the compiler doesn't guarantee alignment or packing but I
feel that as long as I let the compiler deal with the fields then it should
be ok?
Also, I read that there can be a "speed" hit when using bitfields? I don't
really see how this can be true since it only involves a few logical
operators and shifts? (Maybe in specific instances where one is looping over
something that runs in a few cycles where these extra instructions could be
significant might it be a big deal but I don't see why in general)
So, if there are at times a speed issues... for example, lets say that I run
my program and it turns out by using the "bit fields" I get a speed hit...
is there any way to force the compiler to turn those "bit fields" into
standard types(such as int)
i.e.
lets say I have
struct blah
{
unsigned a : 4;
unsigned b : 1;
unsigned d : 9;
}
and I want to "increase the speed", could I tell it to "implicitly" convert
the bit fields into ints? something like
struct blah
{
unsigned a : 8;
unsigned b : 8;
unsigned d : 16;
}
or even
struct blah
{
unsigned a : 32;
unsigned b : 32;
unsigned d : 32;
}
?
Thanks
Jon