Fastest Method to Split a 32 Bit number into Bytes in C++

后端 未结 7 1205
梦谈多话
梦谈多话 2021-01-21 03:15

I am writing a piece of code designed to do some data compression on CLSID structures. I\'m storing them as a compressed stream of 128 bit integers. However, the code in questio

7条回答
  •  迷失自我
    2021-01-21 03:58

    You should measure rather than guess at any potential improvement but my first thought is that it may be faster to do a union as follows:

    typedef union {
        DWORD d;
        struct {
            BYTE b0;
            BYTE b1;
            BYTE b2;
            BYTE b3;
        } b;
    } DWB;
    
    std::vector compBytes;
    DWB invLen;
    invLen.d = (DWORD) invalidClsids.length();
    compBytes.push_back(invalidLength.b.b3);
    compBytes.push_back(invalidLength.b.b2);
    compBytes.push_back(invalidLength.b.b1);
    compBytes.push_back(invalidLength.b.b0);
    

    That may be the right order for the pushbacks but check just in case - it depends on the endian-ness of the CPU.

提交回复
热议问题