设置的最低有效位的位置

Position of least significant bit that is set(设置的最低有效位的位置)
本文介绍了设置的最低有效位的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在寻找一种有效的方法来确定设置在整数中的最低有效位的位置,例如对于 0x0FF0,它将是 4.

一个简单的实现是这样的:

unsigned GetLowestBitPos(无符号值){断言(值!= 0);//单独处理无符号位置 = 0;while (!(value & 1)){值>>=1;++位置;}返回位置;}

任何想法如何从中挤出一些周期?

(注意:这个问题是针对喜欢这种东西的人,而不是告诉我xyz优化是邪恶的.)

[edit] 感谢大家的想法!我也学到了一些其他的东西.酷!

解决方案

Bit Twiddling Hacks

a> 提供了一个很好的集合,呃,一些小技巧,并附有性能/优化讨论.对于您的问题(来自该站点),我最喜欢的解决方案是乘法和查找":

unsigned int v;//在 32 位 v 中找到尾随零的数量国际r;//结果在这里static const int MultiplyDeBruijnBitPosition[32] ={0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >>27];

有用的参考资料:

  • "使用 de Bruijn 序列索引计算机单词中的 1"- 解释上述代码为何有效.
  • "董事会代表 >位板 >比特扫描"- 对该问题的详细分析,特别关注国际象棋编程

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 implementation is this:

unsigned GetLowestBitPos(unsigned value)
{
   assert(value != 0); // handled separately

   unsigned pos = 0;
   while (!(value & 1))
   {
      value >>= 1;
      ++pos;
   }
   return pos;
}

Any ideas how to squeeze some cycles out of it?

(Note: this question is for people that enjoy such things, not for people to tell me xyzoptimization is evil.)

[edit] Thanks everyone for the ideas! I've learnt a few other things, too. Cool!

解决方案

Bit Twiddling Hacks offers an excellent collection of, er, bit twiddling hacks, with performance/optimisation discussion attached. My favourite solution for your problem (from that site) is multiply and lookup:

unsigned int v;  // find the number of trailing zeros in 32-bit v 
int r;           // result goes here
static const int MultiplyDeBruijnBitPosition[32] = 
{
  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 
  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];

Helpful references:

  • "Using de Bruijn Sequences to Index a 1 in a Computer Word" - Explanation about why the above code works.
  • "Board Representation > Bitboards > BitScan" - Detailed analysis of this problem, with a particular focus on chess programming

这篇关于设置的最低有效位的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(将 RGB 转换为 HSV 并将 HSV 转换为 RGB 的算法,范围为 0-255)
How to convert an enum type variable to a string?(如何将枚举类型变量转换为字符串?)
When to use inline function and when not to use it?(什么时候使用内联函数,什么时候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意义;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相关吗?)