[英]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. 我们需要将使用unsigned char作为位掩码的程序升级到使用unsigned short作为位掩码的较新版本。 I believe there is some difference between them because our program failed using same logic with unsigned char changed to unsigned short.
我相信它们之间存在一些差异,因为我们的程序使用相同的逻辑与unsigned char失败变为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: 我们当前设置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
. 将位掩码的值设置为
8192
,但使用unsigned char时,该值将设置为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
). unsigned short是2个字节长,位掩码是
0010000000000000
( 0x2000
)但是对于unsigned char,这个值将是10000000
( 0x80
)。
I believe there is some difference between( unsigned short and unsigned char)
sizeof( unsigned char
) = 1 bytes. sizeof(
unsigned char
)= 1个字节。
sizeof( unsigned short
) = 2 bytes. sizeof(
unsigned short
)= 2个字节。
“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 char)= 1 Byte and Range = 0 - > +255。
sizeof(unsigned short) = 2 Bytes and Range = 0 -> +65,535. sizeof(unsigned short)= 2 Bytes and Range = 0 - > +65,535。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.