Swift solution


  • 0
    class Solution {
        func rangeBitwiseAnd(_ m: Int, _ n: Int) -> Int {
            if m == 0 {
                return 0
            }
            
            var m = m
            var n = n
            var factor = 1
            
            while m != n {
                m >>= 1
                n >>= 1
                factor <<= 1
            }
            
            return m * factor
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.