next up previous

4.2.4 Relative Fitness and Survival

To compute the fitness of an individual we need to count up the number of 1 bits in its genome. Also, if recessive mutations are handled differently than dominant mutations, we want to count the heterozygous loci (loci with one 1 bit) separately from homozygous loci (loci with two 1 bits).

Some machines have a special ``population count'' instruction that will return the number of 1 bits in a word. If you are daring (and don't care too much about portability) you can figure out how to write an assembly code procedure or generate machine code in-line and use this instruction to count the number of mutations in a strand.

An efficient counting procedure can be implemented in C by counting up the bits one byte at a time. Initialize an array bit_count so that bit_count[i] is the number of 1 bits in the binary representation of the character i. For example, the 8-bit number 226 is E2 in hex, which is 11100010 in binary, so set bit_count[226] to 4.