> I am making an updated version of an application that uses bit fields.
> For instance, there are variables of type unsigned int max : 10
> unsigned int rng : 3 unsigned int sec : 8
>
> These values will be written to a binary file, which must be able to
> be read in by another existing application.  So, for instance, there
> must only be 3 bits written to file for variable 'Rng', or the other
> application won't read the data in correctly.  This can't be changed.
>
> First, should I change the values which are of size 8 bits to small
> ints, and forget using bit fields for those?

What do you mean by a small int?  You mean a short?  If so, a short is 16
bits on 32-bit platforms.  In MSVC, a char is 8 bits.

>  Can I
> depend on a small int to be 8 bits, 20 years from now?

Nope.  The C++ standard is too graceful to compiler writers when it comes to
specifying built-in types sizes.  The *only* thing that it requires
specifically is a char being one byte (note: not necessarily 8 bits.)  It
just requires a short be greater than or equal to a char, and a long greater
than or equal to a short.  An int is either the same as short or the same as
long according to the implementation.

As an example, on Win64, a long is 64 bits, a short is 32 bits (IIRC), and a
char is 8 bits.  So if for example you need 16 bits, then you don't want to
use short blindly.

> Second, what is the best way of dealing with the other variables which
> are bit fields (different than 8 bits), in cases such as assignment?
> Should I make a typedef for each different bit size, then use a
> static_cast?

That's a matter of your preference.

I personally wouldn't use bit fields at all.  I would manage the bit
shifting and all the gory details myself, and make sure my code is well
commented and well protected by preprocessor constructs so that I make sure
anyone who reads/uses my code is aware that it's nothing portable.

-------------
Ehsan Akhgari

Farda Technology (http://www.farda-tech.com/)

List Owner: [EMAIL PROTECTED]

[ Email: [EMAIL PROTECTED] ]
[ WWW: http://www.beginthread.com/Ehsan ]

They have something of which they are proud. What do they call it, that
which makes them proud? Culture, they call it; it distinguishes them from
the goatherds.
-Thus Spoke Zarathustra, F. W. Nietzsche





Reply via email to