4
votes

We need to upgrade our program which use unsigned char as bit mask to a newer version which use unsigned short as bit mask. I believe there is some difference between them because our program failed using same logic with unsigned char changed to unsigned short. ( That is from the external library we bought. The library upgraded so we need to change the program too ).

Old version:
typedef struct SomeStruct {
    unsigned char   bit_mask;
#       define      SomeStruct_a_present 0x80
#       define      SomeStruct_b_present 0x40
#       define      SomeStruct_c_present 0x20
    X          x;
    Y          y;
    A          a;  /* optional; set in bit_mask
                                * SomeStruct_a_present if
                                * present */

    B          b;  /* optional; set in bit_mask
                                * SomeStruct_b_present if
                                * present */

    C          c;  /* optional; set in bit_mask
                                * SomeStruct_c_present if
                                * present */
} SomeStruct;



New version:
typedef struct SomeStruct {
    unsigned short   bit_mask;
#       define      SomeStruct_x_present 0x8000
#       define      SomeStruct_y_present 0x4000
#       define      SomeStruct_a_present 0x2000
#       define      SomeStruct_b_present 0x1000
#       define      SomeStruct_c_present 0x0800
    X          x;/* optional; set in bit_mask
                                * SomeStruct_x_present if
                                * present */
    Y          y;/* optional; set in bit_mask
                                * SomeStruct_y_present if
                                * present */
    A          a;  /* optional; set in bit_mask
                                * SomeStruct_a_present if
                                * present */

    B          b;  /* optional; set in bit_mask
                                * SomeStruct_b_present if
                                * present */

    C          c;  /* optional; set in bit_mask
                                * SomeStruct_c_present if
                                * present */
} SomeStruct;

I think there are some problem with our current line because the program crash:

Our current method to set the bit_mask:

someStruct.bit_mask = SomeStruct_a_present;
someStruct.bit_mask |= SomeStruct_b_present;
someStruct.bit_mask |= SomeStruct_c_present;
4
Where does the program crash? - alk

4 Answers

6
votes

With unsigned short this line

someStruct.bit_mask = SomeStruct_a_present;

will set value of bitmask to 8192, but with unsigned char the value will be set to 128.

Reason:

unsigned short is 2 bytes long and bit mask will be 0010000000000000 (0x2000) however with unsigned char this value will be 10000000 (0x80).

4
votes
 I believe there is some difference between( unsigned short and unsigned char)

sizeof(unsigned char) = 1 bytes.

sizeof(unsigned short) = 2 bytes.

1
votes

The length of the "unsigned char" is 1 Byte, and the length of the "unsigned short" is 2

0
votes

From the link here under the MODIFIERS section, you can see that,

sizeof(unsigned char) = 1 Byte and Range = 0 -> +255.

sizeof(unsigned short) = 2 Bytes and Range = 0 -> +65,535.

Ideone Output