nr的比特设置为“1”。在一个字节 [英] nr of bits set to "1" in a byte

查看:109
本文介绍了nr的比特设置为“1”。在一个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找绝对最快的方式

来计算字符串中设置为1

的nr位数。据推测,我首先需要

最快的方式来实现这一点。


我认为就是这样,但欢迎任何改进:


i = 0;

if(g&& 1)i ++;

if(g&& 2)i ++;

if(g&& 3)i ++;

if(g&& 4)i ++;

if(g& & 5)i ++;

if(g&& 6)i ++;

if(g&& 7)i ++;

if(g&& 8)i ++;

解决方案

aku< ak *@europe.com>写道:

我正在寻找绝对最快的方法来计算字符串中设置为1的nr位数。据推测,我首先需要以最快的方式在一个字节内完成此操作。

我认为就是这样,但欢迎任何改进:

i = 0;
if(g&& 1)i ++;
if(g&& 2)i ++;
if(g&& 3)i ++;
if (g&& 4)i ++;
if(g&& 5)i ++;
if(g&& 6)i ++;
if(g&& ; 7)i ++;
if(g&& 8)i ++;




你试过吗?这可能不是最快的方式,

它不起作用。

-

只是另一个C黑客。


aku写道:

我正在寻找绝对最快的方法
来计算设置为nr的位数。 1
在一个字符串中。据推测,我首先需要以最快的方式在一个字节内完成此操作。

我认为就是这样,但欢迎任何改进:

i = 0;
if(g&& 1)i ++;
if(g&& 2)i ++;
if(g&& 3)i ++;
if (g&& 4)i ++;
if(g&& 5)i ++;
if(g&& 6)i ++;
if(g&& ; 7)i ++;
if(g&& 8)i ++;



除了Ben的准确评论,你的代码不会做你所做的事情。 >
期望,表查找通常被认为是绝对最快的方式

做某事。但是,你在速度方面获得的是数据大小的损失,

这是一个经典的交易。在一个字节的范围内,这仍然是一个可管理的表:


int bitsPerByte [] =

{

0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,

1,2, 2,3,2,3,3,4,2,3,3,4,3,4,5,5,

1,2,2,3,2,3,3, 4,2,3,3,4,3,4,4,5,

2,3,3,4,3,4,4,5,3,4,4,5, 4,5,5,6,

/ *等我迟到了* /

}


任何给定的unsigned char g,你用bitPerByte [g]得到的位数设置为1




-

Bertrand Mollinier Toublet

- Le top-posting。

- Quelle est la pratique la plus chiante sur Usenet?


< blockquote> aku< ak *@europe.com>写道:

我正在寻找绝对最快的方法来计算字符串中设置为1的nr位数。据推测,我首先需要以最快的方式在一个字节中执行此操作。
我认为就是这样,但欢迎任何改进:
i = 0;
if(g&& 1)i ++;
if(g&& 2) i ++;
if(g&& 3)i ++;
if(g&& 4)i ++;
if(g&& 5)i ++;
if(g&& 6)i ++;
if(g&& 7)i ++;
if(g&& 8)i ++;




你真的尝试过吗?它甚至都不接近。


即使你纠正了明显的错误并且改变了逻辑AND和按位AND(&),它仍然是不正确的。


下面是一个4位无符号整数,如果设置了相应的位,则值为




[0] [0] [0] [0]

8 4 2 1


现在,再试一次,最好使用CHAR_BIT和循环。

当你做对了,随意优化。


Alex


I''m looking for the absolute fastest way
to count the nr of bits that are set to "1"
in a string. Presumably I then first need the
fastest way to do this in a byte.

I think this is it, but welcome any improvements:

i = 0;
if (g && 1) i++;
if (g && 2) i++;
if (g && 3) i++;
if (g && 4) i++;
if (g && 5) i++;
if (g && 6) i++;
if (g && 7) i++;
if (g && 8) i++;

解决方案

aku <ak*@europe.com> writes:

I''m looking for the absolute fastest way
to count the nr of bits that are set to "1"
in a string. Presumably I then first need the
fastest way to do this in a byte.

I think this is it, but welcome any improvements:

i = 0;
if (g && 1) i++;
if (g && 2) i++;
if (g && 3) i++;
if (g && 4) i++;
if (g && 5) i++;
if (g && 6) i++;
if (g && 7) i++;
if (g && 8) i++;



Did you try it? Not only is that probably not the fastest way,
it doesn''t work.
--
Just another C hacker.


aku wrote:

I''m looking for the absolute fastest way
to count the nr of bits that are set to "1"
in a string. Presumably I then first need the
fastest way to do this in a byte.

I think this is it, but welcome any improvements:

i = 0;
if (g && 1) i++;
if (g && 2) i++;
if (g && 3) i++;
if (g && 4) i++;
if (g && 5) i++;
if (g && 6) i++;
if (g && 7) i++;
if (g && 8) i++;


Besides Ben''s accurate remark that your code will not do what you
expect, table lookup is generally considered the "absolute fastest way"
to do something. What you gain in speed, though, you lose in data size,
which is a classical tradeof. At the scale of a byte, this is still a
manageable table:

int bitsPerByte[] =
{
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
/* etc. I am getting lazy */
}

For any given unsigned char g, you get the number of bits set to 1 in g
with bitsPerByte[g];

--
Bertrand Mollinier Toublet
- Le top-posting.
- Quelle est la pratique la plus chiante sur Usenet ?


aku <ak*@europe.com> wrote:

I''m looking for the absolute fastest way
to count the nr of bits that are set to "1"
in a string. Presumably I then first need the
fastest way to do this in a byte. I think this is it, but welcome any improvements: i = 0;
if (g && 1) i++;
if (g && 2) i++;
if (g && 3) i++;
if (g && 4) i++;
if (g && 5) i++;
if (g && 6) i++;
if (g && 7) i++;
if (g && 8) i++;



Did you actually try this? It is not even close.

Even if you corrected the obvious mistake and changed
logical AND to bitwise AND (&), it is still incorrect.

Below is a 4 bit unsigned integer and the the values
that it would have if the corresponding bit was set.

[ 0 ][ 0 ][ 0 ][ 0 ]
8 4 2 1

Now, try again, preferably by using CHAR_BIT and a loop.
When you get it right, feel free to optimize.

Alex


这篇关于nr的比特设置为“1”。在一个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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