bit_manipulation.count_number_of_one_bits ========================================= .. py:module:: bit_manipulation.count_number_of_one_bits Functions --------- .. autoapisummary:: bit_manipulation.count_number_of_one_bits.benchmark bit_manipulation.count_number_of_one_bits.get_set_bits_count_using_brian_kernighans_algorithm bit_manipulation.count_number_of_one_bits.get_set_bits_count_using_modulo_operator Module Contents --------------- .. py:function:: benchmark() -> None Benchmark code for comparing 2 functions, with different length int values. Brian Kernighan's algorithm is consistently faster than using modulo_operator. .. py:function:: get_set_bits_count_using_brian_kernighans_algorithm(number: int) -> int Count the number of set bits in a 32 bit integer >>> get_set_bits_count_using_brian_kernighans_algorithm(25) 3 >>> get_set_bits_count_using_brian_kernighans_algorithm(37) 3 >>> get_set_bits_count_using_brian_kernighans_algorithm(21) 3 >>> get_set_bits_count_using_brian_kernighans_algorithm(58) 4 >>> get_set_bits_count_using_brian_kernighans_algorithm(0) 0 >>> get_set_bits_count_using_brian_kernighans_algorithm(256) 1 >>> get_set_bits_count_using_brian_kernighans_algorithm(-1) Traceback (most recent call last): ... ValueError: the value of input must not be negative .. py:function:: get_set_bits_count_using_modulo_operator(number: int) -> int Count the number of set bits in a 32 bit integer >>> get_set_bits_count_using_modulo_operator(25) 3 >>> get_set_bits_count_using_modulo_operator(37) 3 >>> get_set_bits_count_using_modulo_operator(21) 3 >>> get_set_bits_count_using_modulo_operator(58) 4 >>> get_set_bits_count_using_modulo_operator(0) 0 >>> get_set_bits_count_using_modulo_operator(256) 1 >>> get_set_bits_count_using_modulo_operator(-1) Traceback (most recent call last): ... ValueError: the value of input must not be negative