Path: utzoo!attcan!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!usc!jarthur!nntp-server.caltech.edu!juliet.caltech.edu!gceych From: gceych@juliet.caltech.edu (Eychaner, Glenn C.) Newsgroups: comp.lang.c Subject: Re: which bits are set Message-ID: <1990Dec18.171839.15135@nntp-server.caltech.edu> Date: 18 Dec 90 17:04:30 GMT References: <3047:Dec1618:51:1590@kramden.acf.nyu.edu> <1990Dec17.071404.6544@kithrup.COM> <15598:Dec1804:57:0390@kramden.acf.nyu.edu> <1990Dec18.093905.17196@kithrup.COM> Sender: news@nntp-server.caltech.edu Reply-To: gceych@juliet.caltech.edu Organization: California Institute of Technology Lines: 42 News-Software: VAX/VMS VNEWS 1.3-4x Nntp-Posting-Host: juliet.caltech.edu In article <1990Dec18.093905.17196@kithrup.COM>, sef@kithrup.COM (Sean Eric Fagan) writes... >In article <15598:Dec1804:57:0390@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes: >>Eek. Why do you want to make this so slow? > Ok, to throw my two cents in, this is the algorithm I use... int bit_set_array [SIZE]; void test_for_bits (int test_int) { int j = 0; while (test_int) { bit_set_array [j] = test_int & 1; test_int >>= 1; j++; } for (; j < SIZE; j++) bit_set_array [j] = 0; } Note that this has the advantage of dropping out if test_int is small. Alternatively: while (j < SIZE) { has the advantage of not filling the array at the end. (I hope this is right. It's a conversion from my parity calculation program, which is:) parity = 0; while (test_int) { parity ^= test_int & 1; test_int >>= 1; } return (parity); I love bitwise operators! Glenn Eychaner |Eychaner@SunCub.Caltech.edu |Remember: It is easier to ride a 40386 N Shore Ln |gceych@iago.caltech.edu |camel through the eye of a needle Big Bear City, CA| Big Bear Solar Observatory |than to drive a Buick through the 92314| !*** G O N I N E R S ***! |hole in a doughnut.