Problem statement

https://binarysearch.com/problems/Bitwise-AND-of-Range-of-Numbers/

Solution

Equal to Leetcode 0201 Bitwise AND of Numbers Range.

Complexity

It is O(log n) for time and space.

Code

class Solution:
    def solve(self, m, n):
        if n == m or m == 0:
            return m
        elif len(bin(m)) != len(bin(n)):
            return 0
        else:
            pref = 1<<(len(bin(m))-3)
            return pref ^ self.solve(m - pref, n - pref)