naman sharma date october 2 2023 task to find the largest power of 2 less than or equal to a given number implementation notes use bit manipulation we start from 1 left shift the set bit to check if res1number each left bit shift represents a pow of 2 for example number 15 res 1 0b1 2 0b10 4 0b100 8 0b1000 16 0b10000 exit return the largest power of two less than or equal to a number largestpowoftwolenum0 0 largestpowoftwolenum1 1 largestpowoftwolenum1 0 largestpowoftwolenum3 2 largestpowoftwolenum15 8 largestpowoftwolenum99 64 largestpowoftwolenum178 128 largestpowoftwolenum999999 524288 largestpowoftwolenum99 9 traceback most recent call last typeerror input value must be a int type return the largest power of two less than or equal to a number largest_pow_of_two_le_num 0 0 largest_pow_of_two_le_num 1 1 largest_pow_of_two_le_num 1 0 largest_pow_of_two_le_num 3 2 largest_pow_of_two_le_num 15 8 largest_pow_of_two_le_num 99 64 largest_pow_of_two_le_num 178 128 largest_pow_of_two_le_num 999999 524288 largest_pow_of_two_le_num 99 9 traceback most recent call last typeerror input value must be a int type
def largest_pow_of_two_le_num(number: int) -> int:
    
    if isinstance(number, float):
        raise TypeError("Input value must be a 'int' type")
    if number <= 0:
        return 0
    res = 1
    while (res << 1) <= number:
        res <<= 1
    return res


if __name__ == "__main__":
    import doctest

    doctest.testmod()