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;
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
).
I believe there is some difference between( unsigned short and unsigned char)
sizeof( unsigned char
) = 1 bytes.
sizeof( unsigned short
) = 2 bytes.
“unsigned char”的长度是1 Byte,“unsigned short”的长度是2
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.