Submask range count
Your task
A radio licence is issued as one bit pattern. The set may tune to a channel number only when every set bit of that number also appears in the licence, so the legal channels are exactly the patterns that fit inside mask. Given mask and an inclusive range from lo to hi, return how many legal channel numbers lie in that range. A licence can carry up to 30 bits and a range can cover most of a billion numbers, so neither listing the legal patterns nor walking the range will finish in time. All three values are non-negative and below 2^30, and a range where lo is greater than hi is empty and returns 0.
Examples
count_licensed_channels(mask: int, lo: int, hi: int) -> intExample 1
Input
count_licensed_channels(5, 0, 5)Output
4Example 2
Input
count_licensed_channels(5, 2, 4)Output
1Example 3
Input
count_licensed_channels(0, 0, 0)Output
1