Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
* <ul>
* <li><b>Finding Common Prefix in Binary Representation:</b>
* <p>The bitwise AND operation identifies common bit patterns among numbers. When applied to a range of numbers, it effectively highlights the highest common bits, essentially finding the "common prefix" of the binary representations within the range.</p>
* <p>This is useful in cases where you need to compress data or perform range-based operations, such as in networking (for subnet calculations), IP range checks, or optimizing certain computations that rely on binary prefixes.</p>
* </li>
* <li><b>Efficient Range Queries:</b>
* <p>In some algorithmic problems, you might want to check commonalities between consecutive numbers in a range. Instead of iterating over the entire range and performing individual AND operations, this algorithm helps compress that into a single step, improving performance.</p>
* </li>
* <li><b>Logical Grouping of Numbers:</b>
* <p>In certain mathematical or data compression problems, grouping numbers based on their common binary patterns can be helpful. The bitwise AND of numbers over a range can act as a first step toward identifying such groups.</p>
* </li>
* <li><b>Optimization in Programming:</b>
* <p>This operation can save time in situations where you're performing repetitive bitwise operations over large data sets, as it helps identify the "stable" bits across numbers in a range without computing it for every single pair.</p>
* </li>
* </ul>
* For more information, refer to the
* <a href="https://en.wikipedia.org/wiki/Bitwise_operation">Bitwise operation</a> Wikipedia page.
*
* <b>Example usage:</b>
* <pre>
* int result = BitWiseAndInRange.rangeBitwiseAnd(5, 7);
* System.out.println("Bitwise AND of range [5, 7]: " + result); // Output: 4
* </pre>
* @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;
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}