which bits are set
Eychaner, Glenn C.
gceych at juliet.caltech.edu
Wed Dec 19 04:04:30 AEST 1990
In article <1990Dec18.093905.17196 at kithrup.COM>, sef at kithrup.COM (Sean Eric Fagan) writes...
>In article <15598:Dec1804:57:0390 at kramden.acf.nyu.edu> brnstnd at 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 at SunCub.Caltech.edu |Remember: It is easier to ride a
40386 N Shore Ln |gceych at 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.
More information about the Comp.lang.c
mailing list