Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between unsigned char and unsigned short in this case?

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;
like image 415
lamwaiman1988 Avatar asked Dec 21 '22 15:12

lamwaiman1988


1 Answers

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).

like image 68
perilbrain Avatar answered Dec 24 '22 01:12

perilbrain