Position of least significant bit that is set

后端 未结 23 994
时光取名叫无心
时光取名叫无心 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 09:09

    According to the Chess Programming BitScan page and my own measurements, subtract and xor is faster than negate and mask.

    (Note than if you are going to count the trailing zeros in 0, the method as I have it returns 63 whereas the negate and mask returns 0.)

    Here is a 64-bit subtract and xor:

    unsigned long v;  // find the number of trailing zeros in 64-bit v 
    int r;            // result goes here
    static const int MultiplyDeBruijnBitPosition[64] = 
    {
      0, 47, 1, 56, 48, 27, 2, 60, 57, 49, 41, 37, 28, 16, 3, 61,
      54, 58, 35, 52, 50, 42, 21, 44, 38, 32, 29, 23, 17, 11, 4, 62,
      46, 55, 26, 59, 40, 36, 15, 53, 34, 51, 20, 43, 31, 22, 10, 45,
      25, 39, 14, 33, 19, 30, 9, 24, 13, 18, 8, 12, 7, 6, 5, 63
    };
    r = MultiplyDeBruijnBitPosition[((uint32_t)((v ^ (v-1)) * 0x03F79D71B4CB0A89U)) >> 58];
    

    For reference, here is a 64-bit version of the negate and mask method:

    unsigned long v;  // find the number of trailing zeros in 64-bit v 
    int r;            // result goes here
    static const int MultiplyDeBruijnBitPosition[64] = 
    {
      0, 1, 48, 2, 57, 49, 28, 3, 61, 58, 50, 42, 38, 29, 17, 4,
      62, 55, 59, 36, 53, 51, 43, 22, 45, 39, 33, 30, 24, 18, 12, 5,
      63, 47, 56, 27, 60, 41, 37, 16, 54, 35, 52, 21, 44, 32, 23, 11,
      46, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6
    };
    r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x03F79D71B4CB0A89U)) >> 58];
    

提交回复
热议问题