Skip to content

Commit 811d754

Browse files
feat: Add BitWiseAndInRange algo with JUnit tests
1 parent 596c614 commit 811d754

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* [BcdConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java)
2727
* [BinaryPalindromeCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheck.java)
2828
* [BitSwap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java)
29+
* [BitWiseAndInRange](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitWiseAndInRange.java)
2930
* [BooleanAlgebraGates](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGates.java)
3031
* [ClearLeftmostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBit.java)
3132
* [CountLeadingZeros](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountLeadingZeros.java)
@@ -664,6 +665,7 @@
664665
* [BcdConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java)
665666
* [BinaryPalindromeCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheckTest.java)
666667
* [BitSwapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java)
668+
* [BitWiseAndInRangeTest](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitWiseAndInRangeTest.java)
667669
* [BooleanAlgebraGatesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGatesTest.java)
668670
* [ClearLeftmostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBitTest.java)
669671
* [CountLeadingZerosTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountLeadingZerosTest.java)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides a method to compute the bitwise AND of all integers within a given range [m, n].
5+
*
6+
* The Bitwise AND of Numbers Range is a useful algorithm that computes the bitwise AND of all integers within a given range [m, n].
7+
*
8+
** Use Cases and Applications:
9+
* <ul>
10+
* <li><b>Finding Common Prefix in Binary Representation:</b>
11+
* <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>
12+
* <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>
13+
* </li>
14+
* <li><b>Efficient Range Queries:</b>
15+
* <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>
16+
* </li>
17+
* <li><b>Logical Grouping of Numbers:</b>
18+
* <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>
19+
* </li>
20+
* <li><b>Optimization in Programming:</b>
21+
* <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>
22+
* </li>
23+
* </ul>
24+
* For more information, refer to the
25+
* <a href="https://en.wikipedia.org/wiki/Bitwise_operation">Bitwise operation</a> Wikipedia page.
26+
*
27+
* <b>Example usage:</b>
28+
* <pre>
29+
* int result = BitWiseAndInRange.rangeBitwiseAnd(5, 7);
30+
* System.out.println("Bitwise AND of range [5, 7]: " + result); // Output: 4
31+
* </pre>
32+
* @author Tanmay Singh
33+
*/
34+
public final class BitWiseAndInRange {
35+
private BitWiseAndInRange() {
36+
}
37+
38+
/**
39+
* Computes the bitwise AND of all integers within the given range [m, n].
40+
*
41+
* @param m the start of the range (inclusive)
42+
* @param n the end of the range (inclusive)
43+
* @return the bitwise AND of all integers in the range [m, n]
44+
*/
45+
public static int rangeBitwiseAnd(int m, int n) {
46+
while (m < n) {
47+
// Turn off the rightmost 1-bit of n
48+
n = n & (n - 1);
49+
}
50+
return n;
51+
}
52+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* Unit tests for the BitWiseAndInRange class.
9+
*/
10+
public class BitWiseAndInRangeTest {
11+
12+
/**
13+
* Test the rangeBitwiseAnd method with a range that includes multiple numbers.
14+
*/
15+
@Test
16+
public void testRangeBitwiseAndMultipleNumbers() {
17+
int result = BitWiseAndInRange.rangeBitwiseAnd(5, 7);
18+
assertEquals(4, result); // Bitwise AND of range [5, 7] should be 4
19+
}
20+
21+
/**
22+
* Test the rangeBitwiseAnd method with a range that includes a single number.
23+
*/
24+
@Test
25+
public void testRangeBitwiseAndSingleNumber() {
26+
int result = BitWiseAndInRange.rangeBitwiseAnd(5, 5);
27+
assertEquals(5, result); // Bitwise AND of range [5, 5] should be 5
28+
}
29+
30+
/**
31+
* Test the rangeBitwiseAnd method with a range that includes zero.
32+
*/
33+
@Test
34+
public void testRangeBitwiseAndIncludesZero() {
35+
int result = BitWiseAndInRange.rangeBitwiseAnd(0, 1);
36+
assertEquals(0, result); // Bitwise AND of range [0, 1] should be 0
37+
}
38+
39+
/**
40+
* Test the rangeBitwiseAnd method with a large range.
41+
*/
42+
@Test
43+
public void testRangeBitwiseAndLargeRange() {
44+
int result = BitWiseAndInRange.rangeBitwiseAnd(0, 2147483647);
45+
assertEquals(0, result); // Bitwise AND of range [0, 2147483647] should be 0
46+
}
47+
48+
/**
49+
* Test the rangeBitwiseAnd method with a range that includes negative numbers.
50+
*/
51+
@Test
52+
public void testRangeBitwiseAndNegativeNumbers() {
53+
int result = BitWiseAndInRange.rangeBitwiseAnd(-5, -3);
54+
assertEquals(-8, result); // Bitwise AND of range [-5, -3] should be -8
55+
}
56+
}

0 commit comments

Comments
 (0)