Java solution using bit operator


  • 0
    C
           /**
    	 * 1. each number odd number has last bit equal to 1 and even as 0
    	 * 2. if m == 4 and n = 14 then there has to be a number between 4 and 14 which has
    	 * last bit equal to 1 
    	 * 3. this is the reason we can right shift both m and n at each iteration of while loop as long as m != n;
    	 */
    	public int rangeBitwiseAnd(int m, int n) {
    		int leftShift = 0;
    
    		while (m != 0 && m != n) {
    			m = m >> 1;
    			n = n >> 1;
    			leftShift++;
    		}
    
    		if (m == 0) {
    			return m;
    		}
    
    		return m << leftShift;
    	}
    

Log in to reply
 

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