测试是否只设置了一位... [英] testing if just one bit is set...

查看:65
本文介绍了测试是否只设置了一位...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

猜测你有一个可以原生处理32位的处理器,而你有一个32位的int
。我现在正在寻找一些*超快*的方法,以确定是否一个int有多个位设置

。任何想法?

解决方案

" .rhavin grobert" < cl *** @ yahoo.dewrote in message

news:ec *************************** ******* @ u29g2000 pro.googlegroups.com ...


猜你有一个处理器可以原生处理32位你有

a 32-bit-int。我现在正在寻找一些*超快*的方法,以确定是否一个int有多个位设置

。有任何想法吗?



如果n有无符号类型(即unsigned int或unsigned long),则(n& -n)

等于n除非n设置了多个位。

所以你要找的表达式是n!=(n& -n)


.. rhavin grobert写道:


猜你有一个可以原生处理32位的处理器,你有32美元的b $ ba $ bit-int。我现在正在寻找一些*超快*的方法,以确定是否一个int有多个位设置

。有任何想法吗?



我认为二元搜索是指方法很快,但你需要

来测量它。类似


inline bool moreThanOneBitSet(无符号值,

unsigned mask1,unsigned mask2)

{

return(value& mask1)&& (值& mask2);

}


bool moreThanOneBitSet(无符号值)

{

static const unsigned masks [] = {0x0000FFFF,0xFFFF0000,

0x00FF00FF,0xFF00FF00,

0x0F0F0F0F,0xF0F0F0F0,

0x33333333,0xCCCCCCCC,

0x55555555,0xAAAAAAAA};

if(value 0){

返回moreThanOneBitSet(value,掩码[0],掩码[1])| |

moreThanOneBitSet(value,masks [2],mask [3])||

moreThanOneBitSet(value,masks [4],mask [5])||

moreThanOneBitSet(value,masks [6],mask [7])||

moreThanOneBitSet(value,masks [8],mask [9]);

}

其他

返回false;

}


最多10按位AND,5逻辑AND,5逻辑OR,并且不确定

许多测试对0和跳跃...


V

-

请在通过电子邮件回复时删除资金''A'

我不回复最热门的回复,请不要问


Andrew Koenig写道:


" .rhavin grobert" < cl *** @ yahoo.dewrote in message

news:ec *************************** ******* @ u29g2000 pro.googlegroups.com ...


>猜测你有一个可以处理32位本地处理器的处理器你有
一个32位的int。我现在正在寻找一些*超快*的方法来确定一个int是否有多个位设置。有任何想法吗?



如果n有无符号类型(即unsigned int或unsigned long),则(n& -n)

等于n除非n设置了多个位。

所以你要找的表达式是n!=(n& -n)



哇...它是否适用于任何表示(两个补码,一个'

补码,签名幅度)?


V

-

请在通过电子邮件回复时删除资金''A'

我不回复热门帖子回复,请不要问


guess you have a processor that can handle 32bit natively and you have
a 32-bit-int. im now looking for some *ultrafast* way to determine if
an int has more than one bit set. any ideas?

解决方案

".rhavin grobert" <cl***@yahoo.dewrote in message
news:ec**********************************@u29g2000 pro.googlegroups.com...

guess you have a processor that can handle 32bit natively and you have
a 32-bit-int. im now looking for some *ultrafast* way to determine if
an int has more than one bit set. any ideas?

If n has an unsigned type (i.e. unsigned int or unsigned long), then (n&-n)
is equal to n unless n has more than one bit set.
So the expression you''re looking for is n!=(n&-n)


..rhavin grobert wrote:

guess you have a processor that can handle 32bit natively and you have
a 32-bit-int. im now looking for some *ultrafast* way to determine if
an int has more than one bit set. any ideas?

I think the "binary search" method is quick enough, but you will need to
measure it. Something like

inline bool moreThanOneBitSet(unsigned value,
unsigned mask1, unsigned mask2)
{
return (value & mask1) && (value & mask2);
}

bool moreThanOneBitSet(unsigned value)
{
static const unsigned masks[] = { 0x0000FFFF,0xFFFF0000,
0x00FF00FF,0xFF00FF00,
0x0F0F0F0F,0xF0F0F0F0,
0x33333333,0xCCCCCCCC,
0x55555555,0xAAAAAAAA };
if (value 0) {
return moreThanOneBitSet(value, masks[0], masks[1]) ||
moreThanOneBitSet(value, masks[2], masks[3]) ||
moreThanOneBitSet(value, masks[4], masks[5]) ||
moreThanOneBitSet(value, masks[6], masks[7]) ||
moreThanOneBitSet(value, masks[8], masks[9]);
}
else
return false;
}

At most, 10 bitwise AND, 5 logical AND, 5 logical OR, and not sure how
many tests against 0 and jumps...

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Andrew Koenig wrote:

".rhavin grobert" <cl***@yahoo.dewrote in message
news:ec**********************************@u29g2000 pro.googlegroups.com...

>guess you have a processor that can handle 32bit natively and you have
a 32-bit-int. im now looking for some *ultrafast* way to determine if
an int has more than one bit set. any ideas?


If n has an unsigned type (i.e. unsigned int or unsigned long), then (n&-n)
is equal to n unless n has more than one bit set.
So the expression you''re looking for is n!=(n&-n)

Wow... Does it work for any representation (two''s complement, one''s
complement, signed magnitude)?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于测试是否只设置了一位...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆