@@ -0,0 +1,1748 @@
+import org.junit.After;%0Aimport org.junit.Assert;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0A/**%0A * Given a range %5Bm, n%5D where 0 %3C= m %3C= n %3C= 2147483647, return the bitwise AND of all numbers in this range,%0A * inclusive.%0A * %3Cp%3E%0A * For example, given the range %5B5, 7%5D, you should return 4.%0A * %3Cp%3E%0A * Tags: Bit Manipulation%0A */%0Apublic class BitwiseAndOfNumbersRange %7B%0A%0A    private BitwiseAndOfNumbersRange b;%0A%0A    /**%0A     * The AND result of the last bit of odd and even numbers should be 0.%0A     * So if m and n are not equal, check the last bit of them.%0A     * Count the number of iterations.%0A     * When m and n are equal, it means there are no more odd and even pairs within the range.%0A     * m has been divided by 2 for count times, so the result it m * count.%0A     */%0A    public int rangeBitwiseAnd(int m, int n) %7B%0A        if (m == 0) return 0;%0A        int moveFactor = 1;%0A        while (m != n) %7B%0A            m %3E%3E= 1;%0A            n %3E%3E= 1;%0A            moveFactor %3C%3C= 1;%0A        %7D%0A        return m * moveFactor;%0A    %7D%0A%0A    @Before%0A    public void setUp() %7B%0A        b = new BitwiseAndOfNumbersRange();%0A    %7D%0A%0A    @Test%0A    public void testEdgeCases() %7B%0A        Assert.assertEquals(0, b.rangeBitwiseAnd(0, 0));%0A        Assert.assertEquals(0, b.rangeBitwiseAnd(0, 1));%0A        Assert.assertEquals(0, b.rangeBitwiseAnd(0, Integer.MAX_VALUE));%0A        Assert.assertEquals(Integer.MAX_VALUE, b.rangeBitwiseAnd(Integer.MAX_VALUE, Integer.MAX_VALUE));%0A    %7D%0A%0A    @Test%0A    public void testExamples() %7B%0A        Assert.assertEquals(0, b.rangeBitwiseAnd(1, 7));%0A        Assert.assertEquals(4, b.rangeBitwiseAnd(5, 7));%0A        Assert.assertEquals(6, b.rangeBitwiseAnd(6, 7));%0A    %7D%0A%0A    @After%0A    public void tearDown() %7B%0A        b = null;%0A    %7D%0A%0A%7D%0A

add Bitwise AND of Numbers Range