Counting Rare Bits When working with wheels, you will frequently need to count the numbers in an intersection of two sets, i.e. in how many numbers they match. Typically, that is a small number of numbers. So when sets are represented as bitmaps, only a few bits are set to 1, most are 0. Here is a fast way to count them in such case. Take a 32-bit example (call it x):
Subtract 1, you get:
Then, "x and (x-1)" is:
You have cut down one bit. So, the following loop will count them all: Count := 0;
Or, even when set bits are not rare, you might wish only to check if their number is not above some limit: for i:=1 to limit do x := x and (x-1);
In any case, the spent time is only proportional to the number of bits to count, not to the size of the whole set.
Making Bits Rare When testing a wheel, e.g. C(16,6,4,5), you need to match a "way", e.g. W={2,4,6,7,8}, to a "game", e.g. G={1,2,3,4,6,7}. When these are stored as bitmaps (e.g. in left-to-right order here), you would have: The intersection, containing 4 numbers {2,4,6,7}, would have 4 bits to count: Instead, you can store the "game" as inverse, i.e. set each bit to 0 if the number is in and to 1 if it's not: The intersection now only contains number 8, which is in W but not in G, so there is only 1 (=5-4) bit to count: In general with C(v,k,t,m), there is typically t > m-t for most practically useful wheels, so using this inversion will speed up the testing, when you count bits as suggested above ("Counting Rare Bits").
Combination Ordinal Numbers
Top | Home | What are wheels | About Ininuga | Links | Free download | Ordinal numbers | Code tips |