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 3 3384
oh, I'd like to mention one more thing, does C++ pack bitfields together?
I have two bitfields that are not aligned such as
struct A
{
unsigned a : 1;
}
strubt B
{
unsigned b : 1;
}
now C++ should pack them into 1 int instead of 2, will it do this?
i.e., will it treat the structures as one such as
struct C
{
unsigned a : 1;
unsigned b : 1;
} ?
(I am using visual studio.net)
The point is that I will potentially have lots of little objects in my
application that have bit fields(properties) "attached" to them and I see no
need to waste space on them all.
Lets say I have 1000 objects with 15 properties each and an overhead of
about 8 bytes per object.
so thtats 1000*(8 + 15*4) = 68000 bytes if using int(32-bit) fields while if
I pack them I'd probably save about 1/5 of that(or more).
I know 68k isn't much but I see no need to waste space any more than I have
to... maybe I am being anal about this? (and the 1000 though can be up to
10k or even more(though I doubt it would be close to 100k).
Maybe the extra computation time is not worth the savings in memory? (I
doubt my application would ever use more than 20 megs of memory but it might
be ran along side of an app that is a memory hog(and also a cpu hog).
Jon Slaughter wrote: oh, I'd like to mention one more thing, does C++ pack bitfields together?
I have two bitfields that are not aligned such as
struct A { unsigned a : 1; }
strubt B { unsigned b : 1; }
now C++ should pack them into 1 int instead of 2, will it do this?
i.e., will it treat the structures as one such as
struct C { unsigned a : 1; unsigned b : 1; } ?
Have you tried to do a sizeof() of the three? You'll know!
I know 68k isn't much but I see no need to waste space any more than I have to... maybe I am being anal about this? (and the 1000 though can be up to 10k or even more(though I doubt it would be close to 100k).
Maybe the extra computation time is not worth the savings in memory? (I doubt my application would ever use more than 20 megs of memory but it might be ran along side of an app that is a memory hog(and also a cpu hog).
Try to use different data structures.
Jon Slaughter wrote: 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?
There are a few corner cases (signed 1-bit). Don't
worry about those. The other common portability mistake
is saving the structure as sizeof(struct) bytes to disk
and assuming another program can read those same bytes
back. That's a form of "indirectly messing".
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).
Both methods are just as portable, altough bitfields could
be slightly faster.
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?
Yes.
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)
In general, writing one field in a bitfield means you need
a read-modify-write cycle. The compiler ensures you don't
actually overwrite other bits. If you've programmed the
equivalent bit ops, you'll understand this - most people
get this wrong the first time.
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?
What about simply removing the : 4 ? That would make a
an unsigned int with the "natural size". Of course, that
would mean that an instruction like ++a does something
different (doesn't wrap at 15)
HTH,
Michiel Salters This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Grumfish |
last post by:
In order to familiarize my self with Flash files and their bytecode I've
started to make an assembler. My first problem is writing the bitfields
the format uses often. It is a series of fields,...
|
by: tmartsum |
last post by:
I have a discussion in comp.std.c++
After a (bit stupid) suggestion on representing a fixed
'big' length int I moderated it to
"Bitfields-ints should be allowed to have any fixed length"
I...
|
by: Régis Troadec |
last post by:
Hi all,
I follow c.l.c. for only a short time and I would like to know
why there isn't anything concerning bitfields among the FAQs.
Is it because ...
1. of portability issues?
2. bitfields...
|
by: rohit |
last post by:
Hi,
In my couple of years of experience, I have never found a single
instance where I needed to use unions and bitfields(though I have used
structures).I was just imagining where would these find...
|
by: GalenTX |
last post by:
I am looking for opinions on a possible approach to coping with
bitfields in a heterogenous environment.
We've got a bunch of legacy code which maps to hardware using
bitfields. The code was...
|
by: Mehta Shailendrakumar |
last post by:
Hi,
I would like to know why array of bitfields is not possible.
Is there any relation with processor architecture for this?
Thank you for your time.
Regards,
Shailendra
|
by: richard_l |
last post by:
Hello All,
I am writing an application which receives a word which is a bitmap. I
have created a word typedef which contains a bitfield defining each
bit. however, I was wondering however if it...
|
by: cman |
last post by:
Who can explain to me what bitfields are and how to use them in
practice? I would need a fairly detailed explaination, I would be a
newbie to advanced C programming features. What are the...
|
by: lithiumcat |
last post by:
Hi,
This question seems to come up quite often, however I haven't managed
to find an answer relevant to my case.
I often use binary flags, and I have alaways used a "bitmask"
technique, that...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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,...
|
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...
| |