C++ union实现比特序列分段处理

       楼主在编写代码的时候遇到这样一个例子:一个24bit的序列需要分成多段进行独立赋值,之后再对整体进行其他处理。主要问题是 如果使用vector容器,第一步的赋值容易,但第二步的整体处理需要先转化为string,再转为uint32处理,步骤很繁琐;如果直接按多段uint32赋值,则需要将多段数据整合为一个uint32处理,也比较麻烦。

此时使用union关键字,目的是赋值的时候可以每段分别独立赋值,处理时又可以作为一个整体来处理

union关键字的含义请参照站内文章,这里只说明使用情景。https://blog.csdn.net/liht_1634/article/details/124737746?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522172301337116800184194888%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=172301337116800184194888&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-2-124737746-null-null.142^v100^pc_search_result_base3&utm_term=union&spm=1018.2226.3001.4187icon-default.png?t=N7T8https://blog.csdn.net/liht_1634/article/details/124737746?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522172301337116800184194888%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=172301337116800184194888&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-2-124737746-null-null.142%5Ev100%5Epc_search_result_base3&utm_term=union&spm=1018.2226.3001.4187

使用union关键字来实现此目的的关键在于内存地址的共用。

union lib_wifi5_l_sig_filed_t
{
    struct
    {
        uint32 RATE         : 4;
        uint32 R            : 1;
        uint32 LENGTH       : 12;
        uint32 P            : 1;
        uint32 SIGNAL_TALL  : 6;
    }bits;
    uint32 value;
};

void test()
{
    lib_wifi5_l_sig_filed_t lsig = {0};//初始化为0
    //分段赋值
    lsig.bits.RATE   = 0b1011;
    lsig.bits.R      = 0;
    lsig.bits.length = 0b101101110001;
    lsig.bits.SIGNAL_TALL = 0;
    //整体操作
    uint32 check_seq = lsig.value & 0x1FFFF;//取前17bit
    lsig.bits.P = deal_parity_check(check_seq);//校验
    uint32 result = lsig.value & 0xffffff;//取24bit
}

如上图代码所示,在union中bits与value内存首地址一致,故分段赋值时使用bits,整体操作时使用value即可。

  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++实现大文件的分段读写可以通过以下骤进行: 1. 打开文件:使用C++的文件流对象,如`std::ifstream`和`std::ofstream`,打开需要读取或写入的文件。 2. 确定文件大小:可以使用文件流对象的`seekg`和`tellg`函数来确定文件的大小。`seekg`函数用于将文件指针移动到指定位置,`tellg`函数用于获取当前文件指针的位置。 3. 分段读取:根据需要设置每次读取的分段大小,可以使用`read`函数从文件中读取指定大小的数据块。读取的数据可以存储在缓冲区中供后续处理。 4. 分段写入:根据需要设置每次写入的分段大小,可以使用`write`函数将数据块写入文件中。写入的数据可以从缓冲区中获取。 5. 关闭文件:在读取或写入完成后,使用文件流对象的`close`函数关闭文件。 下面是一个示例代码,演示了如何实现大文件的分段读写: ```cpp #include <iostream> #include <fstream> void splitFile(const std::string& inputFile, const std::string& outputFile, int segmentSize) { std::ifstream inFile(inputFile, std::ios::binary); std::ofstream outFile(outputFile, std::ios::binary); if (!inFile || !outFile) { std::cout << "Failed to open file!" << std::endl; return; } // 获取文件大小 inFile.seekg(0, std::ios::end); std::streampos fileSize = inFile.tellg(); inFile.seekg(0, std::ios::beg); // 分段读取和写入 char* buffer = new char[segmentSize]; std::streampos bytesRead = 0; while (bytesRead < fileSize) { int bytesToRead = (fileSize - bytesRead < segmentSize) ? (fileSize - bytesRead) : segmentSize; inFile.read(buffer, bytesToRead); outFile.write(buffer, bytesToRead); bytesRead += bytesToRead; } delete[] buffer; inFile.close(); outFile.close(); std::cout << "File split successfully!" << std::endl; } int main() { std::string inputFile = "input.txt"; std::string outputFile = "output.txt"; int segmentSize = 1024; // 每次读取或写入的分段大小 splitFile(inputFile, outputFile, segmentSize); return 0; } ``` 请注意,以上示例代码仅演示了如何实现大文件的分段读写,并未处理异常情况和错误检查。在实际应用中,需要根据具体需求进行适当的错误处理和异常处理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值