Position of least significant bit that is set

后端 未结 23 1264
时光取名叫无心
时光取名叫无心 2020-11-22 08:46

I am looking for an efficient way to determine the position of the least significant bit that is set in an integer, e.g. for 0x0FF0 it would be 4.

A trivial impleme

23条回答
  •  自闭症患者
    2020-11-22 08:56

    It can be done with a worst case of less than 32 operations:

    Principle: Checking for 2 or more bits is just as efficient as checking for 1 bit.

    So for example there's nothing stopping you from checking for which grouping its in first, then checking each bit from smallest to biggest in that group.

    So...
    if you check 2 bits at a time you have in the worst case (Nbits/2) + 1 checks total.
    if you check 3 bits at a time you have in the worst case (Nbits/3) + 2 checks total.
    ...

    Optimal would be to check in groups of 4. Which would require in the worst case 11 operations instead of your 32.

    The best case goes from your algorithms's 1 check though to 2 checks if you use this grouping idea. But that extra 1 check in best case is worth it for the worst case savings.

    Note: I write it out in full instead of using a loop because it's more efficient that way.

    int getLowestBitPos(unsigned int value)
    {
        //Group 1: Bits 0-3
        if(value&0xf)
        {
            if(value&0x1)
                return 0;
            else if(value&0x2)
                return 1;
            else if(value&0x4)
                return 2;
            else
                return 3;
        }
    
        //Group 2: Bits 4-7
        if(value&0xf0)
        {
            if(value&0x10)
                return 4;
            else if(value&0x20)
                return 5;
            else if(value&0x40)
                return 6;
            else
                return 7;
        }
    
        //Group 3: Bits 8-11
        if(value&0xf00)
        {
            if(value&0x100)
                return 8;
            else if(value&0x200)
                return 9;
            else if(value&0x400)
                return 10;
            else
                return 11;
        }
    
        //Group 4: Bits 12-15
        if(value&0xf000)
        {
            if(value&0x1000)
                return 12;
            else if(value&0x2000)
                return 13;
            else if(value&0x4000)
                return 14;
            else
                return 15;
        }
    
        //Group 5: Bits 16-19
        if(value&0xf0000)
        {
            if(value&0x10000)
                return 16;
            else if(value&0x20000)
                return 17;
            else if(value&0x40000)
                return 18;
            else
                return 19;
        }
    
        //Group 6: Bits 20-23
        if(value&0xf00000)
        {
            if(value&0x100000)
                return 20;
            else if(value&0x200000)
                return 21;
            else if(value&0x400000)
                return 22;
            else
                return 23;
        }
    
        //Group 7: Bits 24-27
        if(value&0xf000000)
        {
            if(value&0x1000000)
                return 24;
            else if(value&0x2000000)
                return 25;
            else if(value&0x4000000)
                return 26;
            else
                return 27;
        }
    
        //Group 8: Bits 28-31
        if(value&0xf0000000)
        {
            if(value&0x10000000)
                return 28;
            else if(value&0x20000000)
                return 29;
            else if(value&0x40000000)
                return 30;
            else
                return 31;
        }
    
        return -1;
    }
    

提交回复
热议问题