Snippet: bitcount()

Klaus Alexander Seistrup kas at maps.magnetic-ink.dk
Fri Jul 16 11:43:21 EDT 1999


  G'day mates.

Here's a snippet for the repository:

# The function bitcount() will count the number
# of bits in a number, iterating at most as many
# times as the number of bits.
#
def bitcount(x):
    n = 0
    while x:
        x = x & (x-1)
        n = n + 1
    # end while
    return n
# end def bitcount


Cheers,

  //Klaus

-- 
···[ Magnetic Ink ]·················································· ><> ···
···[ http://www.magnetic-ink.dk/ ]···········································




More information about the Python-list mailing list