怎么把两个uint8_t的变量存到uint16_t的变量里,其中一个uint_8占uint16_t的高八位,另一个占低八位
时间: 2023-08-12 09:01:15 浏览: 176
可以使用位运算和类型转换来实现将两个 `uint8_t` 类型的变量存储到一个 `uint16_t` 类型变量的高八位和低八位中。
下面是一个示例代码:
```cpp
uint8_t highByte = 0xAB; // 高八位
uint8_t lowByte = 0xCD; // 低八位
uint16_t combinedValue = ((uint16_t)highByte << 8) | lowByte;
```
在这个示例中,我们首先将 `highByte` 强制转换为 `uint16_t` 类型,然后将其左移 8 位,使其占据 `uint16_t` 变量的高八位。接着,我们使用按位或操作符 `|` 将 `lowByte` 存储在 `combinedValue` 的低八位中。
最终,`combinedValue` 的高八位将是 `highByte` 的值(0xAB),低八位将是 `lowByte` 的值(0xCD)。
相关问题
uint8_t mHighestVoltgeCellSubSysNum; uint8_t mHighestVoltgeCellNum; uint16_t mHighestVoltgeCellVal; uint8_t mLowestVoltgeCellSubSysNum; uint8_t mLowestVoltgeCellNum; uint16_t mLowestVoltgeCellVal; uint8_t mHighestTmprtureSubSysNum; uint8_t mHighestTmprtureProbNum; uint8_t mHighestTmprtureVal; uint8_t mLowestTmprtureSubSysNum; uint8_t mLowestTmprtureProbNum; uint8_t mLowestTmprtureVal;写一下上面所有变量的set、get函数
以下是上述所有变量的set和get函数的示例代码:
```cpp
// Set and Get functions for mHighestVoltgeCellSubSysNum
void setHighestVoltageCellSubSysNum(uint8_t value) {
mHighestVoltgeCellSubSysNum = value;
}
uint8_t getHighestVoltageCellSubSysNum() {
return mHighestVoltgeCellSubSysNum;
}
// Set and Get functions for mHighestVoltgeCellNum
void setHighestVoltageCellNum(uint8_t value) {
mHighestVoltgeCellNum = value;
}
uint8_t getHighestVoltageCellNum() {
return mHighestVoltgeCellNum;
}
// Set and Get functions for mHighestVoltgeCellVal
void setHighestVoltageCellValue(uint16_t value) {
mHighestVoltgeCellVal = value;
}
uint16_t getHighestVoltageCellValue() {
return mHighestVoltgeCellVal;
}
// Set and Get functions for mLowestVoltgeCellSubSysNum
void setLowestVoltageCellSubSysNum(uint8_t value) {
mLowestVoltgeCellSubSysNum = value;
}
uint8_t getLowestVoltageCellSubSysNum() {
return mLowestVoltgeCellSubSysNum;
}
// Set and Get functions for mLowestVoltgeCellNum
void setLowestVoltageCellNum(uint8_t value) {
mLowestVoltgeCellNum = value;
}
uint8_t getLowestVoltageCellNum() {
return mLowestVoltgeCellNum;
}
// Set and Get functions for mLowestVoltgeCellVal
void setLowestVoltageCellValue(uint16_t value) {
mLowestVoltgeCellVal = value;
}
uint16_t getLowestVoltageCellValue() {
return mLowestVoltgeCellVal;
}
// Set and Get functions for mHighestTmprtureSubSysNum
void setHighestTemperatureSubSysNum(uint8_t value) {
mHighestTmprtureSubSysNum = value;
}
uint8_t getHighestTemperatureSubSysNum() {
return mHighestTmprtureSubSysNum;
}
// Set and Get functions for mHighestTmprtureProbNum
void setHighestTemperatureProbNum(uint8_t value) {
mHighestTmprtureProbNum = value;
}
uint8_t getHighestTemperatureProbNum() {
return mHighestTmprtureProbNum;
}
// Set and Get functions for mHighestTmprtureVal
void setHighestTemperatureValue(uint8_t value) {
mHighestTmprtureVal = value;
}
uint8_t getHighestTemperatureValue() {
return mHighestTmprtureVal;
}
// Set and Get functions for mLowestTmprtureSubSysNum
void setLowestTemperatureSubSysNum(uint8_t value) {
mLowestTmprtureSubSysNum = value;
}
uint8_t getLowestTemperatureSubSysNum() {
return mLowestTmprtureSubSysNum;
}
// Set and Get functions for mLowestTmprtureProbNum
void setLowestTemperatureProbNum(uint8_t value) {
mLowestTmprtureProbNum = value;
}
uint8_t getLowestTemperatureProbNum() {
return mLowestTmprtureProbNum;
}
// Set and Get functions for mLowestTmprtureVal
void setLowestTemperatureValue(uint8_t value) {
mLowestTmprtureVal = value;
}
uint8_t getLowestTemperatureValue() {
return mLowestTmprtureVal;
}
```
这些函数可根据需要在您的代码中使用,用于设置和获取相应的变量值。请注意,这些示例代码仅提供了函数的框架,您需要根据您的具体需求进行适当的实现。
C语言 ,将一个uint16_t变量赋予uint8_t数组
可以使用位运算符和类型转换来实现。具体实现代码如下:
uint16_t var = 1234;
uint8_t arr[2];
arr[0] = (uint8_t)(var & 0xFF); // 取低8位存放在arr[0]
arr[1] = (uint8_t)((var >> 8) & 0xFF); // 取高8位存放在arr[1]
通过位运算符和类型转换,可以将一个16位的变量分别存储在两个8位的数组中。
阅读全文