【发布时间】:2023-04-05 04:30:01
【问题描述】:
我有一个在循环中返回布尔值(8 次)的函数,我想构造一个 uint8_t,你可能猜到我是 C 和 C++ 新手。
unsigned int data[8];
for(int i = 0; i < 8; i++) {
// 1 or 0 if switch is on or off
int value = (int)functionThatReturnsBool(i);
data[i] = value;
}
// Needs B00000000 (with the correct switches on)
functionThatNeeds(uint8_t ???);
【问题讨论】:
-
先定义如何将数组中的值转换为
uint8_t值。 -
只需使用位掩码,根据布尔值设置对应的位即可。
-
我不清楚你到底想达到什么目标,但听起来
std::bitset可能对你有用。
标签: c++ bit-shift uint8array