Modifying Bitfields in CodeView Gives Unexpected Results

ID Number: Q68692

3.00 3.10 3.11 3.14 | 3.00 3.10 3.11 3.12 3.50

MS-DOS | OS/2

buglist3.10 buglist3.11 buglist3.12 buglist3.14 buglist3.50

Summary:

When modifying a bitfield in the Watch or Local windows in CodeView,

the value assigned to the field may not be the same value that the

field is set to. The value placed in the bitfield is assigned to the

whole word that the field is a part of. This value will change for all

bitfields declared in the same word.

In Microsoft C, consecutive bitfields are assigned consecutive

positions in a word (or multiple words) unless padded with an unnamed

bitfield. In that case, the bitfield after the unnamed bitfield is

placed in the next word, as determined by the packing level (/Zp or

pack pragma).

Sample Code

-----------

#include <stdio.h>

struct _bits {

unsigned x : 2; // Bitfield of length 2

unsigned y : 8; // Bitfield of length 8

} bits;

void main(void)

{

printf("In main\n");

}

More Information:

If the code above is debugged and a watch is placed on "bits", the

watch window will resemble the following:

-bits

x: 0x0000

y: 0x0000

If "y" is changed by typing over the value of y in the watch window

with "4", the following is displayed:

-bits

x: 0x0000

y: 0x0001

This may seem strange until you realize that by putting 4 into y, you

are really putting 0x0004 into the double word that makes up the

structure bits. That is:

0 0 0 4 Hex

0000|0000|0000|0100 Binary

yy|yyyy|yyxx Variable

Once you realize this, you can put "0x10" (or 16 in radix 10) into y

to generate the desired result:

-bits

x: 0x0000

y: 0x0004

As a second workaround, you can use the command window to modify the

value directly. In other words, type the following to change the value

of y to 4:

?bits.y=4

Microsoft has confirmed this to be a problem in CodeView versions 3.0,

3.1, 3.11, 3.12, 3.14, and 3.5. We are researching this problem and

will post new information here as it becomes available.

Additional reference words: buglist3.00 3.00 3.10 3.5