Re: How to count zeros in registers?
- From: Spehro Pefhany <speffSNIP@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 01 Dec 2005 11:30:43 -0500
On Thu, 1 Dec 2005 15:12:26 +0000 (UTC), the renowned
kensmith@xxxxxxxxxxxxxxx (Ken Smith) wrote:
>In article <pan.2005.12.01.03.10.44.378533@xxxxxxxxxxx>,
>Rich Grise <rich@xxxxxxxxxxx> wrote:
>[...]
>>NZeros = 0;
>>loop_count = 7; // or maybe this should be 8;
>>While ((X & 0x80) && loop_count) (
>> NZeros++;
>> X <<= 1;
>> loop_count--;
>>}
>
>If there are only 8 bits to worry about:
>
> X := (X and $55) + ((X shr 1) and $55);
> X := (X and $33) + ((X shr 2) and $33);
> X := (X and $07) + ((X shr 4) and $07);
Clever, but doesn't that yield the total number of '1's rather than
the number of leading zeros?
/* return the number of leading zeros, 0x08 if all zeros,
for 0 <= n <= 255 /
int countlz(int n)
{
int i=0;
while ((i < 8) && (0 == (n & 0x80)))
{
i++;
n <<=1;
};
return i;
}
It might be faster to change the while condition to something like
(0==((i | n) & 0x80)), and check for the zero condition first.
Best regards,
Spehro Pefhany
--
"it's the network..." "The Journey is the reward"
speff@xxxxxxxxxxxx Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog Info for designers: http://www.speff.com
.
- Follow-Ups:
- Re: How to count zeros in registers?
- From: Ken Smith
- Re: How to count zeros in registers?
- References:
- Re: How to count zeros in registers?
- From: Jim Thompson
- Re: How to count zeros in registers?
- From: John B
- Re: How to count zeros in registers?
- From: Rich Grise
- Re: How to count zeros in registers?
- From: Ken Smith
- Re: How to count zeros in registers?
- Prev by Date: using a current to measure a time interval
- Next by Date: Re: finding the right mate
- Previous by thread: Re: How to count zeros in registers?
- Next by thread: Re: How to count zeros in registers?
- Index(es):
Relevant Pages
|