|
| 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 | +} |
0 commit comments