diff --git a/DIRECTORY.md b/DIRECTORY.md index d9beaadcba4d..fdd63e8f6728 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -26,6 +26,7 @@ * [BcdConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java) * [BinaryPalindromeCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheck.java) * [BitSwap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java) + * [BitWiseAndInRange](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitWiseAndInRange.java) * [BooleanAlgebraGates](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGates.java) * [ClearLeftmostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBit.java) * [CountLeadingZeros](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountLeadingZeros.java) @@ -666,6 +667,7 @@ * [BcdConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java) * [BinaryPalindromeCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheckTest.java) * [BitSwapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java) + * [BitWiseAndInRangeTest](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitWiseAndInRangeTest.java) * [BooleanAlgebraGatesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGatesTest.java) * [ClearLeftmostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBitTest.java) * [CountLeadingZerosTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountLeadingZerosTest.java) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BitWiseAndInRange.java b/src/main/java/com/thealgorithms/bitmanipulation/BitWiseAndInRange.java new file mode 100644 index 000000000000..d13d3f79d9b2 --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/BitWiseAndInRange.java @@ -0,0 +1,52 @@ +package com.thealgorithms.bitmanipulation; + +/** + * This class provides a method to compute the bitwise AND of all integers within a given range [m, n]. + * + * The Bitwise AND of Numbers Range is a useful algorithm that computes the bitwise AND of all integers within a given range [m, n]. + * + ** Use Cases and Applications: + * + * For more information, refer to the + * Bitwise operation Wikipedia page. + * + * Example usage: + *
+ * int result = BitWiseAndInRange.rangeBitwiseAnd(5, 7);
+ * System.out.println("Bitwise AND of range [5, 7]: " + result); // Output: 4
+ * 
+ * @author Tanmay Singh + */ +public final class BitWiseAndInRange { + private BitWiseAndInRange() { + } + + /** + * Computes the bitwise AND of all integers within the given range [m, n]. + * + * @param m the start of the range (inclusive) + * @param n the end of the range (inclusive) + * @return the bitwise AND of all integers in the range [m, n] + */ + public static int rangeBitwiseAnd(int m, int n) { + while (m < n) { + // Turn off the rightmost 1-bit of n + n = n & (n - 1); + } + return n; + } +} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/BitWiseAndInRangeTest.java b/src/test/java/com/thealgorithms/bitmanipulation/BitWiseAndInRangeTest.java new file mode 100644 index 000000000000..d502da7b5b50 --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/BitWiseAndInRangeTest.java @@ -0,0 +1,56 @@ +package com.thealgorithms.bitmanipulation; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +/** + * Unit tests for the BitWiseAndInRange class. + */ +public class BitWiseAndInRangeTest { + + /** + * Test the rangeBitwiseAnd method with a range that includes multiple numbers. + */ + @Test + public void testRangeBitwiseAndMultipleNumbers() { + int result = BitWiseAndInRange.rangeBitwiseAnd(5, 7); + assertEquals(4, result); // Bitwise AND of range [5, 7] should be 4 + } + + /** + * Test the rangeBitwiseAnd method with a range that includes a single number. + */ + @Test + public void testRangeBitwiseAndSingleNumber() { + int result = BitWiseAndInRange.rangeBitwiseAnd(5, 5); + assertEquals(5, result); // Bitwise AND of range [5, 5] should be 5 + } + + /** + * Test the rangeBitwiseAnd method with a range that includes zero. + */ + @Test + public void testRangeBitwiseAndIncludesZero() { + int result = BitWiseAndInRange.rangeBitwiseAnd(0, 1); + assertEquals(0, result); // Bitwise AND of range [0, 1] should be 0 + } + + /** + * Test the rangeBitwiseAnd method with a large range. + */ + @Test + public void testRangeBitwiseAndLargeRange() { + int result = BitWiseAndInRange.rangeBitwiseAnd(0, 2147483647); + assertEquals(0, result); // Bitwise AND of range [0, 2147483647] should be 0 + } + + /** + * Test the rangeBitwiseAnd method with a range that includes negative numbers. + */ + @Test + public void testRangeBitwiseAndNegativeNumbers() { + int result = BitWiseAndInRange.rangeBitwiseAnd(-5, -3); + assertEquals(-8, result); // Bitwise AND of range [-5, -3] should be -8 + } +}