You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++二进制文件多字节移位与位截断高效实现咨询

技术需求

对二进制.bin文件执行带位截断的移位操作,具体要求:

  • 跳过前16字节(字节编号从0开始),从第17字节中截取第140、141、142位共3位,将后续整个序列按这3位的值进行移位;
  • 完成上述操作后,跳过32字节,再次截取3位并对剩余序列执行移位操作。

待解决问题

  • 无法正确实现跨多字节的移位操作;
  • 程序需满足时间效率要求:1分钟内处理约50MB的文件,因此禁止使用嵌套循环等低效写法。

示例代码

我编写了以下示例代码,思路方向大致正确,但仍未解决跨多字节移位的核心问题:

#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>

std::vector<uint8_t> Shift(std::vector<uint8_t>& src, int val, bool left) {
    std::vector<uint8_t> res(src.size());
    int byteShift = val >> 3;
    uint8_t mask = ~(0b00001110);
    if (byteShift >= src.size())
        return res;
    val = 3;
    res.insert(res.begin(), src.begin(), src.begin() + 17); // Copy the first 17 bytes unchanged
    int j = 1;
    for (size_t i = 17; i < src.size()-j; i++) {
        if ((i - 16) % 32 == 0 && i != 17) {
            res[i + j] = static_cast<uint8_t>(((src[i + j] &= mask)>>3) | (src[i + j + 1] << 5));

            res[i] = static_cast<uint8_t>(((src[i] >> val) | (res[i + j] << 8 - val)));

            switch (val)
            {
            case 1: 
                val = 4;
                break;
            case 2:
                val = 5;
                break;
            case 3:
                val = 6;
                break;
            case 4:
                val = 7;
                break;
            case 5:
                val = 8;
                break;
            case 6:
                val = 1;
                j++;
                break;
            case 7: 
                val = 2;
                j++;
                break;
            case 8:
                val = 3;
                j++;
                break;
            }
        }
        else {
            res[i] = static_cast<uint8_t>((src[i] >> val) | (src[i + j] << (8 - val)));
        }
    }
    return res;
}

int main() {
    const std::string inputFileName = "F:/C++ Progect/ConsoleApplication5/ConsoleApplication5/x64/Debug/0_1.bin";
    const std::string outputFileName = "output.bin";
    const int shiftValue = 3;
    const bool shiftLeft = false; // Change to false for right shift

    try {
        std::ifstream inputFile(inputFileName, std::ios::binary);
        if (!inputFile)
            throw std::runtime_error("The input file could not be opened.");

        inputFile.seekg(0, std::ios::end);
        std::streampos fileSize = inputFile.tellg();
        inputFile.seekg(0, std::ios::beg);

        std::vector<uint8_t> inputData(fileSize);
        inputFile.read(reinterpret_cast<char*>(inputData.data()), fileSize);

        std::vector<uint8_t> shiftedData = Shift(inputData, shiftValue, shiftLeft);

        std::ofstream outputFile(outputFileName, std::ios::binary);
        if (!outputFile)
            throw std::runtime_error("The output file could not be created.");

        outputFile.write(reinterpret_cast<const char*>(shiftedData.data()), shiftedData.size());

        std::cout << "The shift was completed successfully." << std::endl;
    }
    catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

补充说明

提供了源文件0_1.bin和预期结果文件1_1.bin用于验证。

内容的提问来源于stack exchange,提问作者Леонид Ильин

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 05:50:23