Algorithm for copying N bits at arbitrary position from one int to another

前端 未结 7 1769
别那么骄傲
别那么骄傲 2021-01-31 21:36

An interesting problem I\'ve been pondering the past few days is how to copy one integer\'s bits into another integer at a given position in the destination integer. So, for exa

相关标签:
7条回答
  • 2021-01-31 21:58

    I don't think it can be done more efficient unless you write assembler.

    You can improve the readability and solve your overflow problem changing some little things:

    int setbits2(int destination, int source, int at, int numbits)
    {
        // int mask = ((1LL<<numbits)-1)<<at; // 1st aproach
        int mask = ((~0u)>>(sizeof(int)*8-numbits))<<at; // 2nd aproach
        return (destination&~mask)|((source<<at)&mask);
    }
    

    More efficient assembler version (VC++):

    // 3rd aproach
    #define INT_SIZE 32;
    int setbits3(int destination, int source, int at, int numbits)
    { __asm {
        mov ecx, INT_SIZE
        sub ecx, numbits
        or  eax, -1
        shr eax, cl
        mov ecx, at
        shl eax, cl // mask == eax
        mov ebx, eax
        not eax
        and eax, destination
        mov edx, source
        shl edx, cl
        and edx, ebx
        or  eax, edx
    }}
    
    • 1st aproach: Slower on 32bit architecture
    • 2nd aproach: (~0u) and (sizeof(int)*8) are calculated at compile time, so they don't charge any cost.
    • 3rd aproach: You save 3 ops (memory accesses) writing it in assembler but you will need to write ifdefs if you want to make it portable.
    0 讨论(0)
  • 2021-01-31 21:59

    uint32_t copy_bits(uint32_t dst, uint32_t src, uint8_t end_bit, uint8_t start_bit)

    {

    uint32_t left, right, mask, result;
    
    if (end_bit <= start_bit)
    {
        printf("%s: end_bit:%d shall be greater than start_bit: %d\n", __FUNCTION__, end_bit, start_bit);
        return 0;
    }
    
    left   = ~0; // All Fs
    right  = ~0;
    result = 0;
    left  >>= ((sizeof(uint32_t)*8) - end_bit); // Create left half of mask
    right <<= start_bit; // Create right half of mask
    mask   =  (left & right); // Now you have the mask for specific bits
    result = (dst & (~mask)) | (src & (mask));
    printf("%s, dst: 0x%08x, src: 0x%08x, end_bit: %d, start_bit: %d, mask: 0x%08x, result: 0x%08x\n",
          __FUNCTION__, dst, src, end_bit, start_bit, mask, result);
    
    return result;
    

    }

    0 讨论(0)
  • 2021-01-31 22:10

    I think it hardly could be more efficient. Moreover, bitwise operations are much faster than any algebraic operations.

    0 讨论(0)
  • 2021-01-31 22:13

    Generalized GRB-fnieto form...

    template <typename T>
    T setbits4(T destination, T source, int at, int numbits)
    {
        T mask = (((T)-1)>>(sizeof(T)*8-numbits))<<at; // 4th aproach
        return (destination&~mask)|((source<<at)&mask);
    }
    
    0 讨论(0)
  • 2021-01-31 22:20

    It pretty good: I tried this alternate version, but yours was about 30% faster in testing:

        int[] bits = new int[] {0,1,3,7,15,31,63,127,255,511,1023
            ,2047,4095,8192,16383,32767,65535,131071,262143,524287
            ,1048575,2097151,4194303,8388607,16777215,33554431,67108863
            ,134217727,268435455,536870911,1073741823,2147483647,-1};
    
        public int setbits2(int destination, int source, int at, int numbits)
        {
            int ones = bits[numbits + at] & ~bits[at];
            return (destination & ~ones) | ((source << at) & ones);
        }
    
    0 讨论(0)
  • 2021-01-31 22:22

    I don't think it's the case that 1<<32 wraps (otherwise, why doesn't 2<<31 also wrap?), instead I think that internally modulus 32 is applied to the second operator, so that 1<<32 is actually equivalent to 1<<0. Also, consider changing the parameters types from "int" to "unsigned int". To get the value of "ones" without running into the "1<<32" problem, you can do this:

    unsigned int ones = (0xffffffff >> (32-numbits)) << at;
    

    I don't believe there are any "standard" methods for this kind of operation. I'm sure there are other ways of using bitwise operators in different ways to achieve the same outcome, but your algorithm is as good as any.

    Having said that, though, maintainability and documentation is also important. Your function would benefit from the algorithm being documented with a comment, especially to explain how you use the bitwise XOR -- which is clever, but not easy to understand at first glance.

    0 讨论(0)
提交回复
热议问题