【发布时间】:2022-08-03 16:50:27
【问题描述】:
我有一个对我没有意义的 unit32 变量向量。但其中前 16 个字节和后 16 个字节是有意义的。然后我需要将它的索引分隔为 c++ 中的 2 个 unit16 变量。 这个问题我该怎么办?
-
uint16_t first = your_number, second = your_number >> 16;。
标签: c++ vector type-conversion
我有一个对我没有意义的 unit32 变量向量。但其中前 16 个字节和后 16 个字节是有意义的。然后我需要将它的索引分隔为 c++ 中的 2 个 unit16 变量。 这个问题我该怎么办?
uint16_t first = your_number, second = your_number >> 16;。
标签: c++ vector type-conversion
您必须将它们拆分为 uint32 并强制转换。
std::pair<uint16_t, uint16_t> split(uint32_t var) {
return {static_cast<uint16_t>(var >> 16),
static_cast<uint16_t>(var & 0xffff)};
}
【讨论】:
vectoruint32 变量。 vector 甚至被标记。您的代码接受单个变量。
这是一个实现。
vector<uint32_t> a = {/*your data goes here*/};
vector<uint16_t> mostSignificantBits, leastSignificantBits;
for(uint32_t i : a) {
mostSignificantBits.push_back((uint16_t)(i >> 16));
leastSignificantBits.push_back((uint16_t)i);
}
简单地转换它将占用 16 个最低有效位。
为了检索最高有效位,我们使用右移。
【讨论】: