计算一个字节中的'on'位? [英] Counting 'on' bits in a byte?

查看:86
本文介绍了计算一个字节中的'on'位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在一个字节中返回''on''位的数量:

''a''= 01100001 = 3位等等......

我只是在一个名为

''bits''的计数器中添加一个按位AND(''&''对八进制1'),然后移动我的字节1向右;这样做8次。


这样可行,但我很好奇是否有一些更有效的公式

没有循环或递归?这似乎是一个非常多的代码,但

我想不出更好的方法。

int BitCount(char c)

{

int ctr;

int bits = 0;

for(ctr = 0; ctr< 8; ctr ++){

位+ = c& 0001;

c>> = 1;

}

返回(位);

}

解决方案



John写道:


int BitCount(char c)

{

int ctr;

int bits = 0;

for(ctr = 0; ctr < 8; ctr ++){

bits + = c& 0001;

c>> = 1;

}

返回(位);

}



两件事


1.右移签名类型==坏

2.这是真的吗你的应用程序的瓶颈是什么?


但是如果你知道你正在处理字节并且空间只需要使用

一个8x8的查找表。在这里没有提示:BitCount(x& 15)+

BitCount(x>> 4)== BitCount(x)为8位无符号x。


Tom


John(在0G *****************) **@tornado.ohiordc.rr.com)说:


|试图在一个字节中返回''on''位的数量:

| ''a''= 01100001 = 3比特等......

|

|我只是在计数器上添加了一点点的AND(''&''对八进制1)

|称为''bits'',然后将我的字节1位向右移;这样做8

|时间。

|

|这有效,但我很好奇是否有一些更有效的公式

|没有循环或递归?这看起来好像很多

|代码,但我想不出更好的方法。

|

| int BitCount(char c)

| {

| int ctr;

| int bits = 0;

| for(ctr = 0; ctr< 8; ctr ++){

|位+ = c& 0001;

| c>> = 1;

| }

|返回(位);

| } $ / $

int BitCount(unsigned char c)

{int bits = 0;

do bits + = c& 1; while(c>> = 1);

返回位;

}


快速,无循环方法将涉及在
256元素数组中预先存储计数,以便您可以编码:


int BitCount(unsigned char c)

{static unsigned char count [] = {0,1,1,2,1,/ * .. * /,8};

返回计数[(unsigned char)c];

}


-

Morris Dovey

DeSoto Solar

美国爱荷华州德索托
http://www.iedu.com/DeSoto


Morris Dovey(在93*************@news.uswest.net)说:


......丑陋的东西。我打算失去演员阵容。编译器知道如何处理下标的



| int BitCount(unsigned char c)

| {static unsigned char count [] = {0,1,1,2,1,/ * .. * /,8};

返回计数[c];

| }


-

Morris Dovey

DeSoto Solar

DeSoto,爱荷华州美国 http://www.iedu.com/DeSoto

Trying to return the number of ''on'' bits in a byte:
''a'' = 01100001 = 3 on bits, etc...

I''m just adding a bit-wise AND (''&'' against octal 1) to a counter called
''bits'', then shifting my byte 1 bit to the right; do this 8 times.

This works, but I was curious if there''s some more effective formula
without looping or recursion? That seems like an awful lot of code, but
I can''t think of a better way.
int BitCount(char c)
{
int ctr;
int bits = 0;
for (ctr = 0; ctr < 8; ctr++) {
bits += c & 0001;
c >>= 1;
}
return (bits);
}

解决方案


John wrote:

int BitCount(char c)
{
int ctr;
int bits = 0;
for (ctr = 0; ctr < 8; ctr++) {
bits += c & 0001;
c >>= 1;
}
return (bits);
}

Two things

1. Right shifting signed types == bad
2. Is this really the bottleneck in your application?

But if you know you''re dealing with bytes and have the space just use
an 8x8 lookup table. Failing that here''s a hint: BitCount(x&15) +
BitCount(x>>4) == BitCount(x) for 8-bit unsigned "x".

Tom


John (in 0G*******************@tornado.ohiordc.rr.com) said:

| Trying to return the number of ''on'' bits in a byte:
| ''a'' = 01100001 = 3 on bits, etc...
|
| I''m just adding a bit-wise AND (''&'' against octal 1) to a counter
| called ''bits'', then shifting my byte 1 bit to the right; do this 8
| times.
|
| This works, but I was curious if there''s some more effective formula
| without looping or recursion? That seems like an awful lot of
| code, but I can''t think of a better way.
|
| int BitCount(char c)
| {
| int ctr;
| int bits = 0;
| for (ctr = 0; ctr < 8; ctr++) {
| bits += c & 0001;
| c >>= 1;
| }
| return (bits);
| }

int BitCount(unsigned char c)
{ int bits = 0;
do bits += c & 1; while (c >>= 1);
return bits;
}

The fast, no-loop method would involve pre-storing counts in a
256-element array so that you could code:

int BitCount(unsigned char c)
{ static unsigned char count[] = { 0,1,1,2,1, /* .. */ ,8 };
return count[(unsigned char)c];
}

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto


Morris Dovey (in 93*************@news.uswest.net) said:

....ugly stuff. I meant to lose the cast. The compiler knows how to
handle the subscript.

| int BitCount(unsigned char c)
| { static unsigned char count[] = { 0,1,1,2,1, /* .. */ ,8 };
return count[c];
| }

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto


这篇关于计算一个字节中的'on'位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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