Finding MSB in bit string
Michael Tiemann
tiemann at mcc-pp.UUCP
Fri Nov 14 15:16:20 AEST 1986
In article <7143 at boring.mcvax.UUCP>, guido at mcvax.uucp (Guido van Rossum) writes:
> >What's a good general method? How would a real programmer do it?
>
> This has been discussed in this group a while ago. It goes something like
>
> return count[x&0xff] + count[(x>>8)&0xff] +
> count[(x>>16)&0xff] + count[(x>>32)&0xff;
^^
24
>
> (I hope this time I didn't forget some offsets like in my solution of
> the MSB problem.)
Nope, just some arithmetic.
How about
cnt = count[x&0xff];
cnt += count[(x>>=8) & 0xff];
cnt += count[(x>>=8) & 0xff];
return cnt + count[x&0xff];
for those poor souls without barrel shifters.
But this is not a *general* method, since there is no parameterization
of the bit-string (!). Not all bit-strings are 32-bit ints. I won't
*even* touch the issue of bit-string alignment...
Michael
tiemann at mcc.com
More information about the Comp.lang.c
mailing list