Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!im4u!milano!mcc-pp!tiemann From: tiemann@mcc-pp.UUCP (Michael Tiemann) Newsgroups: comp.lang.c Subject: Re: Finding MSB in bit string Message-ID: <2040@mcc-pp.UUCP> Date: Fri, 14-Nov-86 00:16:20 EST Article-I.D.: mcc-pp.2040 Posted: Fri Nov 14 00:16:20 1986 Date-Received: Fri, 14-Nov-86 06:07:51 EST References: <800@oakhill.UUCP> <11053@cca.UUCP> <7143@boring.mcvax.UUCP> Organization: MCC, Austin, TX Lines: 30 Summary: Saving Guy the trouble In article <7143@boring.mcvax.UUCP>, guido@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@mcc.com