找到位数 [英] find number of digits
问题描述
有没有人知道找到一个有效的方法来查找一个数字的
位数(即最重要的位置是1)
二进制数?到目前为止我发现的是:
- digits =(int)log2(number),其中log 2表示记录基数2
- 转移到0 :for(j = 0; number>> = 1; j ++); digits = j;
- 二元搜索转移
任何更好的想法?
谢谢和问候,
Christof
Hi,
does anyone know of an efficient way to find the number of
digits (i.e. the most significant position that is 1) of a
binary number? What I found so far is:
- digits = (int) log2(number), where log 2 means log for base 2
- shifting until 0: for(j=0; number>>=1; j++); digits = j;
- binary search shifting
Any better ideas?
Thanks and regards,
Christof
推荐答案
CHRISTOF WARLICH写道:
CHRISTOF WARLICH wrote:
有没有人知道一种有效的方法来查找>数字的数量(即1的最重要位置)<二进制数?到目前为止我发现的是:
- digits =(int)log2(number),其中log 2表示记录基数2
- 转移到0:for(j = 0; number> ;> = 1; j ++); digits = j;
- 二元搜索转移
任何更好的想法?
感谢和问候,
Christof
Hi,
does anyone know of an efficient way to find the number of
digits (i.e. the most significant position that is 1) of a
binary number? What I found so far is:
- digits = (int) log2(number), where log 2 means log for base 2
- shifting until 0: for(j=0; number>>=1; j++); digits = j;
- binary search shifting
Any better ideas?
Thanks and regards,
Christof
<如果您的二进制数足够小,表格查找就足够了,那么
...
David
if your binary number is small enough, a table lookup may suffice...
David
* CHRISTOF WARLICH:
* CHRISTOF WARLICH:
有没有人知道找到一个有效的方法来查找数字的数字(即最重要的位置是1)
二进制数?到目前为止我发现的是:
- digits =(int)log2(number),其中log 2表示记录基数为2
非常低效,除非你有一个快速整数日志函数
(在这种情况下正是你正在寻找的)。
- 转移到0:for(j = 0;数字>> = 1; j ++); digits = j;
使用++ j,并以stylewise方式添加一个空循环体{}。
- 二分搜索移动
任何更好的想法?
does anyone know of an efficient way to find the number of
digits (i.e. the most significant position that is 1) of a
binary number? What I found so far is:
- digits = (int) log2(number), where log 2 means log for base 2
Very inefficient unless you have a fast integer log function
(which in that case is exactly what you''re looking for).
- shifting until 0: for(j=0; number>>=1; j++); digits = j;
Use ++j, and stylewise add an empty loop body, {}.
- binary search shifting
Any better ideas?
您可以随时使用表格,在二进制搜索中将数字分成块数
,直到适合表格大小。
但这在内存使用方面效率低下。
你没有说明你的意思。有效的。
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门发布。
问:usenet和电子邮件中最烦人的是什么?
You could always use a table, dividing the number up in chunks
in a binary search until suitable for the table size.
But that is inefficient in terms of memory usage.
You don''t specify what you mean by "efficient".
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
CHRISTOF WARLICH写道:
CHRISTOF WARLICH wrote:
有没有人知道找到
数字的有效方法
二进制数的最重要位置是1?到目前为止我发现的是:
- digits =(int)log2(number),其中log 2表示记录基数2
- 转移到0:for(j = 0; number> ;> = 1; j ++); digits = j;
- 二元搜索转移
任何更好的想法?
感谢和问候,
Christof
Hi,
does anyone know of an efficient way to find the number of
digits (i.e. the most significant position that is 1) of a
binary number? What I found so far is:
- digits = (int) log2(number), where log 2 means log for base 2
- shifting until 0: for(j=0; number>>=1; j++); digits = j;
- binary search shifting
Any better ideas?
Thanks and regards,
Christof
有很多ffs的实现。或找到第一组或并且大多数linux boxen上的
它在string.h中以int ffs(int)的形式提供。
然而,这是一个可爱的技巧。将整数转换为浮点
将为您提供在指数中寻找的数字。
std :: frexp()将提取指数...
#include< cmath>
int ffs_fp(int value)
{
if (值== 0)
{
返回-1; //没有回答
}
int exponent;
double correct_mantissa = std :: frexp(value,& exponent) ;
返回指数-1;
}
#include< iostream>
void testit(int value)
{
std :: cout<< ffs_fp("<< value<<")=" << ffs_fp(value)<< " \ n";
}
int main()
{
testit(1<<< 2);
testit(1<< 0);
testit(1<< 17);
testit(-1 +(1<< 17));
}
There are a number of implementations of "ffs" or "find first set" and
on most linux boxen it''s available as int ffs(int) in string.h.
However, this is a cute trick. Converting an integer to floating point
will give you the number you''re looking for in the exponent.
std::frexp() will extract the exponent ...
#include <cmath>
int ffs_fp( int value )
{
if ( value == 0 )
{
return -1; // no answer
}
int exponent;
double correct_mantissa = std::frexp( value, &exponent );
return exponent -1;
}
#include <iostream>
void testit( int value )
{
std::cout << "ffs_fp( " << value << " ) = " << ffs_fp( value ) << "\n";
}
int main()
{
testit( 1 << 2 );
testit( 1 << 0 );
testit( 1 << 17 );
testit( -1 + ( 1 << 17 ) );
}
这篇关于找到位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!