def hamming_weight(x: int):
    """
    Count the number of on bits in an integer.

    Args:
        x: the number to count on bits

    Returns:
        Integer representing the number of bits
        that are on (1).
    """
    count = 0
    while x:
        # bitwise AND number with itself minus 1
        x &= x - 1
        count += 1
    return count