整数变量中有多少位是'1'? [英] How many bits are '1' in a integer variable?
问题描述
嗨伙计们,
我想找一个更有效的方法来计算多少位是
''1''整数变量?。
我仍然不知道除了使用循环和if
语句之外的位数。
您能否知道其他更有效的方式?
Cuthbert
int main(无效)
{
int var = 0xFF0F;
int i,count = 0;
int mask = 1;
for(i = 0; i< sizeof(int)* 8; i ++)
if(mask<< i& var)count ++;
printf("%d \ n",count);
返回0;
}
Cuthbert写道:
嗨伙计们,
我我试图找到一种更有效的方法来计算一个整数变量中有多少位
''1'?
I STI除了使用循环和if
语句之外,我不知道要计算这些位。
你能知道其他更有效的方法吗?
将int分成字节并使用查找表。
-
Thomas M. Sommers - tm*@nj.net - AB2SB
< BLOCKQUOTE> TM Sommers发布:
Cuthbert写道:
>嗨大家好,
>我试图找到一种更有效的方法来计算整数变量中有多少位是'1''?
我仍然不知道数除了使用循环和if
语句之外的位。
你能知道其他更有效的方法吗?
将int分成几个字节并使用查找表。
我肯定有比这更好的方法......甚至可能是一个表达式
产生一个编译时常量。也许类似于:
#define UINT_BITS_SET(x)\
!!((x)& 1U)+ !!((x)& 1U<< ; 1)+ !!((x)& 1U<<<< 2)+ !!((x)& 1U<<<<<<<<<<<<<"> &安培; 1U<< 4)+ !!((X)及1U<&δ)+ !!((X)及1U<&10 6)+ !!((X)及1U<< ; 7)\
+ !!((x)& 1U<< 8)+ !!((x)& 1U<< 9)+ !!((x) & 1U<< 10)+ !!((x)& 1U<< 11)\
+ !!((x)& 1U<< 12)+! !((x)& 1U<< 13)+ !!((x)& 1U<< 14)+ !!((x)& 1U<< 15)\
+ !!((x)& 1U<< 16)+ !!((x)& 1U<< 17)+ !!((x)& 1U<< 18)+! !((x)& 1U<< 19)\
+ !!((x)& 1U<< 20)
我不确定它是否是未定义的行为左移一个无符号的int
多于它的位数;如果没有,那么你可以简单地跟你一样高......
+ !!((x)& 1U<< 63548)
如果是,那么你可以将IMAX_BITS宏与模数和除法运算符结合使用,以保证安全。
-
Frederick Gotham
Cuthbert写道:
嗨伙计们,
a更简单,因此可能更快一点:
int main(无效)
{
int var = 0xFF0F;
int i,count = 0;
for(i = 0; i< sizeof(int)* 8; i ++)
{count + = var& 1; var>> = 1; }
printf("%d \ n",count);
返回0;
}
a字节查找表可能会或可能不会更快,具体取决于缓存中发生的
。
Hi folks,
I am trying to find a more efficient way to count "How many bits are
''1'' in a integer variable?".
I still have no idea to count the bits except using a loop and "if"
statements.
Could you know any other more efficient way?
Cuthbert
int main (void)
{
int var = 0xFF0F;
int i, count = 0;
int mask = 1;
for ( i = 0; i < sizeof(int)*8 ; i++ )
if ( mask<<i & var) count++ ;
printf("%d\n", count);
return 0;
}
Cuthbert wrote:Hi folks,
I am trying to find a more efficient way to count "How many bits are
''1'' in a integer variable?".
I still have no idea to count the bits except using a loop and "if"
statements.
Could you know any other more efficient way?Break the int into bytes and use a lookup table.
--
Thomas M. Sommers -- tm*@nj.net -- AB2SB
T.M. Sommers posted:
Cuthbert wrote:>Hi folks,
I am trying to find a more efficient way to count "How many bits are
''1'' in a integer variable?".
I still have no idea to count the bits except using a loop and "if"
statements.
Could you know any other more efficient way?
Break the int into bytes and use a lookup table.
I''m sure there''s a better way than that... perhaps even an expression
yielding a compile-time constant. Maybe something like:
#define UINT_BITS_SET(x)\
!!((x)&1U) + !!((x)&1U<<1) + !!((x)&1U<<2) + !!((x)&1U<<3)\
+ !!((x)&1U<<4) + !!((x)&1U<<5) + !!((x)&1U<<6) + !!((x)&1U<<7)\
+ !!((x)&1U<<8) + !!((x)&1U<<9) + !!((x)&1U<<10) + !!((x)&1U<<11)\
+ !!((x)&1U<<12) + !!((x)&1U<<13) + !!((x)&1U<<14) + !!((x)&1U<<15)\
+ !!((x)&1U<<16) + !!((x)&1U<<17) + !!((x)&1U<<18) + !!((x)&1U<<19)\
+ !!((x)&1U<<20)
I''m not sure if it''s undefined behaviour to left shift an unsigned int by
more places than it has bits; if not, then you can simply go as high as you
want:
+ !!((x)&1U<<63548)
If it is however, then you could use the IMAX_BITS macro in conjunction
with the modulus and division operators to keep things safe.
--
Frederick Gotham
Cuthbert wrote:Hi folks,
a bit simpler and therefore perhaps somewhat faster:
int main (void)
{
int var = 0xFF0F;
int i, count = 0;
for ( i = 0; i < sizeof(int)*8 ; i++ )
{ count += var & 1; var >>= 1; }
printf("%d\n", count);
return 0;
}
a byte lookup table might or might not be faster, depending on what
happens with the cache.
这篇关于整数变量中有多少位是'1'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!