tags:

views:

159

answers:

3

Hey Folks,

I have a question. I have a legacy application which used bit fields in a structure. Something like this

struct sample
{
    BYTE      one: 2; 
    BYTE   two  : 1;
    BYTE   three: 5;
} sampletest;

So "three" can have a value of MAX 31 only.

Now I have a requirement to increase the MAX value of three. I am planning to remove the bit fields. It will assume more memory now. Apart from this, Is there anything that I need to take care? Does it cause any more other harm?

Thanks in Advance, -Mani.

+3  A: 

You have to carefully look through all the code to check that the code doesn't rely on this structure containing bit fields.

For example it could be that somewhere in the code the whole byte could be read, manipulated and written back (with casts it's not a problem) - if you remove bit fields this code will break apart. So look for casts - they are the indicator of code to check.

sharptooth
Thanks for your comments.-Mani.
Manigandan
+3  A: 

If BYTE is an unsigned type, then the bitfields will have well-defined overflow behavour - eg:

sampletest.one = -1;

will set sampletest.one to 3. This behaviour will change if you make it a wider type.

caf
A: 

There are a few additional things that might be important for you. Or not.

In C language bit fields can only be declared as 'int', 'signed int' or 'unsigned int' (also 'bool' in C99). Any other type is not allowed (or not portable, if your compiler allows it as an extension). (I wonder what hides behind 'BYTE' in your case.)

Also, type 'int' (not explicitly 'signed' or 'unsigned'), when used in a bit-field declaration, might declare either signed or unsigned field, depending on the implementation (and/or its compilation settings).

Since you are planning to convert your 'three' into a ordinary field (not a bit-filed), it might make sense to check whether it was supposed to be signed or unsigned.

AndreyT
BYTE is an unsigned char in my case.
Manigandan