![](/img/trans.png)
[英]Difference between unsigned short and unsigned char in LoRa communication
[英]What is the difference between unsigned char and unsigned short in this case?
我們需要將使用unsigned char作為位掩碼的程序升級到使用unsigned short作為位掩碼的較新版本。 我相信它們之間存在一些差異,因為我們的程序使用相同的邏輯與unsigned char失敗變為unsigned short。 (這是我們購買的外部庫。庫已升級,所以我們也需要更改程序)。
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;
我認為當前行存在一些問題,因為程序崩潰:
我們當前設置bit_mask的方法:
someStruct.bit_mask = SomeStruct_a_present;
someStruct.bit_mask |= SomeStruct_b_present;
someStruct.bit_mask |= SomeStruct_c_present;
用無符號短接這一行
someStruct.bit_mask = SomeStruct_a_present;
將位掩碼的值設置為8192
,但使用unsigned char時,該值將設置為128
。
原因:
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個字節。
sizeof( unsigned short
)= 2個字節。
“unsigned char”的長度是1 Byte,“unsigned short”的長度是2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.