位位置 [英] bit position

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

问题描述

获得最低位的​​最佳位置是什么?

知道1位只有1位?

喜欢位的位置0x8是4,0x4它是3等等。


我能想出的最好的东西是


#define BITPOS(x) \

((x)& 0x1?1:\


(x)& 0x2?2:\


(x)& 0x4?3:\


(x)& 0x8?4:0)


int main(无效){


无符号短x = 0x0008;


而(x){


printf("%d \ n",BITPOS(x));


x>> = 4;


}

返回0;


}

解决方案

服务Laurijssen说:


什么是获得最低位的​​最佳位置

你只知道1位将在1?



如果你有一个n位无符号整数类型,首先将值

与(1<<(&n; / 2)),并使用二进制搜索在位上,为O(log

n)而不是O(n)。


- < br $>
Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)




" ;服务Laurijssen < se*@n.tkha scritto nel messaggio

news:eh ********** @ news5.zwoll1.ov.home.nl ...
< blockquote class =post_quotes>
什么是获得最低位的​​最佳位置





只知道1位将在1上?

在位0x8的位置是4,0x4它是3等等


我能想到的最好的事情是


#define BITPOS(x)\


((x) & 0x1?1:\


(x)& 0x2?2:\


(x)& 0x4? 3:\


(x)& 0x8?4:0)


int main(void){


unsigned short x = 0x0008;


while(x){


printf("%d \ n,BITPOS(x));


x>> = 4;


}


返回0;


}



另一种可能性是'查找表'':


/ *

*为半字节:''最低1'的位置,(如果缺少则为0)

* /

int bitpos [] = {

0,/ * 0 0 0 0 * /

1, / * 0 0 0 1 * /

2,/ * 0 0 1 0 * /

1,/ * 0 0 1 1 * /

3,/ * 0 1 0 0 * /

1,/ * 0 1 0 1 * /

2,/ * 0 1 1 0 * /

1,/ * 0 1 1 1 * /

4,/ * 1 0 0 0 * /

1,/ * 1 0 0 1 * /

2,/ * 1 0 1 0 * /

1,/ * 1 0 1 1 * /

3,/ * 1 1 0 0 * /

1,/ * 1 1 0 1 * /

2,/ * 1 1 1 0 * /

1,/ * 1 1 1 1 * /

};


/ *

*用法

* /

bitp = bitpos [0xF& x];


-

Giorgio Silvestri

DSP /嵌入式/实时操作系统软件工程师




" Serve Laurijssen" < se*@n.tkwrote in message

news:eh ********** @ news5.zwoll1.ov.home.nl ...


获得最低位的​​最佳位置是什么?

你知道1位只有1位吗?

在位0x8的位置是4,0x4它是3等等。



所以这里没有一些出价小提琴解决方案吗? :)


What would be an optimal way to get the position of the lowest bit where you
know only 1 bit will be on 1?
Like in the position of bit 0x8 is 4, 0x4 it''s 3 etc.

Best thing I could come up with is

#define BITPOS(x) \

((x) & 0x1 ? 1 : \

(x) & 0x2 ? 2 : \

(x) & 0x4 ? 3 : \

(x) & 0x8 ? 4 : 0)

int main(void) {

unsigned short x = 0x0008;

while (x) {

printf("%d\n", BITPOS(x));

x >>= 4;

}
return 0;

}

解决方案

Serve Laurijssen said:

What would be an optimal way to get the position of the lowest bit where
you know only 1 bit will be on 1?

If you have an n-bit unsigned integer type, start off by comparing the value
with (1 << (n / 2)), and home in on the bit using binary search, for O(log
n) instead of O(n).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)



"Serve Laurijssen" <se*@n.tkha scritto nel messaggio
news:eh**********@news5.zwoll1.ov.home.nl...

What would be an optimal way to get the position of the lowest bit where

you

know only 1 bit will be on 1?
Like in the position of bit 0x8 is 4, 0x4 it''s 3 etc.

Best thing I could come up with is

#define BITPOS(x) \

((x) & 0x1 ? 1 : \

(x) & 0x2 ? 2 : \

(x) & 0x4 ? 3 : \

(x) & 0x8 ? 4 : 0)

int main(void) {

unsigned short x = 0x0008;

while (x) {

printf("%d\n", BITPOS(x));

x >>= 4;

}
return 0;

}

Another possibility is a ''look-up table'':

/*
* for nibbles: position of ''lowest 1'', (0 if missing)
*/
int bitpos[] = {
0, /* 0 0 0 0 */
1, /* 0 0 0 1 */
2, /* 0 0 1 0 */
1, /* 0 0 1 1 */
3, /* 0 1 0 0 */
1, /* 0 1 0 1 */
2, /* 0 1 1 0 */
1, /* 0 1 1 1 */
4, /* 1 0 0 0 */
1, /* 1 0 0 1 */
2, /* 1 0 1 0 */
1, /* 1 0 1 1 */
3, /* 1 1 0 0 */
1, /* 1 1 0 1 */
2, /* 1 1 1 0 */
1, /* 1 1 1 1 */
};

/*
* usage
*/
bitp = bitpos[0xF & x];

--
Giorgio Silvestri
DSP/Embedded/Real Time OS Software Engineer



"Serve Laurijssen" <se*@n.tkwrote in message
news:eh**********@news5.zwoll1.ov.home.nl...

What would be an optimal way to get the position of the lowest bit where
you know only 1 bit will be on 1?
Like in the position of bit 0x8 is 4, 0x4 it''s 3 etc.

so there isnt some bid fiddle solution to this? :)


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

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