位计算 - 寻找整型中第一个比特1的位置

 记录《hack's delight》 中一个神奇算法。

static const int lsz64_tbl[64] = {

     0, 31,  4, 33, 60, 15, 12, 34,
    61, 25, 51, 10, 56, 20, 22, 35,
    62, 30,  3, 54, 52, 24, 42, 19,
    57, 29,  2, 44, 47, 28,  1, 36,
    63, 32, 59,  5,  6, 50, 55,  7,
    16, 53, 13, 41,  8, 43, 46, 17,
    26, 58, 49, 14, 11, 40,  9, 45,
    21, 48, 39, 23, 18, 38, 37, 27
};

//______________________________________________________________________________
/* FirstPiece():
  *
  *      Return square number (0 to 63) of the least significant set bit
  *      in bitboard 'bb'
  *
  *      source: Matt Taylor's "de Bruijn method" implementation
  *      Probably best if you don't even TRY to understand this one. I certainly don't...
  */
//______________________________________________________________________________
int FirstPiece(const BITBOARD bb) {
    const BITBOARD lsb = (bb & -bb) - 1;
    const unsigned int foldedLSB = ((unsigned int) lsb) ^ ((unsigned int)(lsb >> 32));
    return lsz64_tbl[foldedLSB * 0x78291ACF >> 26];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值