en:pfw:population_20count
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:pfw:population_20count [2023-09-04 18:17] – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1 | en:pfw:population_20count [2023-09-04 18:17] (current) – ↷ Seite von pfw:population_20count nach en:pfw:population_20count verschoben uho | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | {{pfw: | ||
| + | ==== Population Count ==== | ||
| + | |||
| + | Population count, or **popcount**, | ||
| + | |||
| + | For processors which lack such an opcode, and for Forths implementations which lack a popcount primitive, here an algorithms to quickly get the count of set bits. | ||
| + | |||
| + | The following program is around 10 times faster than a simple counting routine using a loop. Working out how this short program works is a very interesting excercise and is left to the user. Lets not spoil all the fun! | ||
| + | |||
| + | == Population Count - Forth routine == | ||
| + | |||
| + | < | ||
| + | hex | ||
| + | : popcount ( number -- no_of_bits_in_number ) | ||
| + | dup 1 rshift 55555555 and - | ||
| + | dup 33333333 and | ||
| + | swap 2 rshift 33333333 and + | ||
| + | dup 4 rshift + | ||
| + | f0f0f0f and | ||
| + | 1010101 * | ||
| + | 18 rshift | ||
| + | ; | ||
| + | decimal | ||
| + | </ | ||
| + | |||
| + | **Population count on Wikipedia: | ||