def count_bits_set(field):
    """Count the number of bits set in an integer

    Discovered independently by:

      - Brian W. Kernighan (C Programming Language 2nd Ed.)
      - Peter Wegner (CACM 3 (1960), 322)
      - Derrick Lehmer (published in 1964 in a book edited by Beckenbach)

    Source: http://www-graphics.stanford.edu/~seander/bithacks.html
    """
    count = 0
    while field:
        field &= field - 1
        count += 1
    return count