如何使用按位操作在 `uint16_t` 上存储和读回多个数值?

How to store into and read back multiple number values on an `uint16_t` using bitwise manipulation?

使用按位运算,是否可以将以下一组值打包并回读到一个uint16_t变量中?我想是的,但我正在尝试弄清楚如何使用示例程序。

假设以下是我要打包到 uint16_t.

中的一组值
unsigned int iVal1 = 165 // which is 8 bits
unsigned int iVal2 = 28  // which is 5 bits
unsigned int iVal3 = 3   // which is 2 bits
bool bVal = true;        // which can stored in 1 bit if we use 0 for true and 1 for false

以下是我的程序,目的是写入值,需要读回。如何使用 C++ 11 写入和读取值?

#include <iostream>

uint16_t Write(unsigned int iVal1, unsigned int iVal2, unsigned int iVal3, bool bVal) {
    // Is this technique correct to package the 3 values into an uint16_t?
    return static_cast<uint16_t>(iVal1) + static_cast<uint16_t>(iVal2) + static_cast<uint16_t>(iVal3) + static_cast<uint16_t>(bVal);
}

unsigned int ReadVal1(const uint16_t theNumber) {
    // How to read back iVal1
}

unsigned int ReadVal2(const uint16_t theNumber) {
    // How to read back iVal2
}

unsigned int ReadVal3(const uint16_t theNumber) {
    // How to read back iVal3
}

bool ReadVal4(const uint16_t theNumber) {
    // How to read back bVal
}

int main() {
    unsigned int iVal1 = 165; // which is 8 bits
    unsigned int iVal2 = 28;  // which is 5 bits
    unsigned int iVal3 = 3;   // which is 2 bits
    bool bVal = true;        // which can stored in 1 bit if we use 0 for true and 1 for false

    const uint16_t theNumber = Write(iVal1, iVal2, iVal3, bVal);

    std::cout << "The first 8 bits contain the number: " << ReadVal1(theNumber) << std::endl;
    std::cout << "Then after 8 bits contain the number: " << ReadVal2(theNumber) << std::endl;
    std::cout << "Then after 2 bits contain the number: " << ReadVal3(theNumber) << std::endl;
    std::cout << "Then after 1 bit contains the number: " << ReadVal4(theNumber) << std::endl;
}

为此,您需要进行位移。

uint16_t Write(unsigned int iVal1, unsigned int iVal2, unsigned int iVal3, bool bVal) {
    // this will encode ival1 on the 8 first bits, ival2 on bits 4 to 8, 
    // ival3 on bits 2 and 3, and bval on last bit
    return (static_cast<uint16_t>(iVal1)<<8) + (static_cast<uint16_t>(iVal2)<<3) + (static_cast<uint16_t>(iVal3)<<1) + static_cast<uint16_t>(bVal);
}

那么您的 uint16_t 将拥有您需要的所有价值。 要回读,假设是 ival2,您需要向后移动并使用 and 运算符:

unsigned int ReadVal1(const uint16_t theNumber) {
    // ival1 is the first 8 bits from place 9 to 16
    uint16_t check1 = 255; // in bits 0000000011111111
    return (theNumber>>8)&check1;
}
unsigned int ReadVal2(const uint16_t theNumber) {
    // ival2 is the 5 bits from place 3 to place 8
    uint16_t check2 = 31; // in bits 0000000000011111
    return (theNumber>>3)&check2;
}
unsigned int ReadVal3(const uint16_t theNumber) {
    // ival3 is the 2 bits on places 2 and 3
    uint16_t check3 = 3; // in bits 0000000000000011
    return (theNumber>>1)&check3;
}
bool ReadVal4(const uint16_t theNumber) {
    // ival4 is the last bit
    uint16_t check4 = 1; // in bits 0000000000000001
    return theNumber&check4;
}

注意:这里 true 等于 1,false 等于 0。