Compute the Bitwise AND of all Numbers in a Range using JavaScript
Last Updated :
20 May, 2024
The complexity of bitwise AND computation for all numbers in a specific range is a common challenge faced by developers, particularly when optimizing algorithms. We have given two integers, L and R, and find the bitwise AND of all numbers in the inclusive range from L to R in JavaScript.
Example:
Let's consider an example where L=3 and R=6.
The bitwise AND of all numbers from 3 to 6 is calculated
as follows: 3&4&5&6 = (3&4) & (5&6)
= 0 & 4
= 0
Below are the approaches to compute the bitwise AND of all numbers in a range using JavaScript:
Bitwise AND Property
In this approach, compute the bitwise AND of all numbers in a given range [L, R]. The algorithm repeatedly clears the least significant bit of 'R' using the operation 'R = R & (R - 1)' until 'L' is no longer less than 'R'. After reducing the range, it performs a final bitwise AND operation between 'L' and the modified 'R' to get the result. This method leverages the property that clearing the least significant bits reduces the range until 'L' and 'R' converge to a common prefix.
Example: The example below shows how to compute the bitwise AND of all numbers in a range using bitwise AND properties in JavaScript.
JavaScript
function rangeBitwiseAnd(L, R) {
while (L < R) {
R = R & (R - 1);
}
return L & R;
}
let L = 3;
let R = 6;
console.log(rangeBitwiseAnd(L, R));
Time Complexity: O(N), where N is the size of the range.
Space Complexity: O(1), as it uses a constant amount of additional space regardless of the input size.
Bitwise Manipulation
In this approach, bits are directly manipulated to compute the AND of numbers in the given range without using any additional data structures. It is based on observing patterns in the bitwise AND operation. The bitwise AND of all numbers in a range [L, R] is computed by right-shifting both L and R until they are equal, while counting the number of shifts. The common prefix is then left-shifted back to its original position. This approach effectively finds the common higher-order bits of the range.
Example: Consider an example where L=4 and R=8. We compute the AND of all numbers from 4 to 8 by observing patterns in the AND operation.
JavaScript
function rangeBitwiseAnd(L, R) {
let shift = 0;
while (L < R) {
L >>= 1;
R >>= 1;
shift++;
}
return L << shift;
}
let L = 4;
let R = 8;
console.log(rangeBitwiseAnd(L, R));
Time Complexity: O(N) It iterates through each number in the range.
Space Complexity: O(1).
Similar Reads
JavaScript Program to Compute the Bitwise OR of all Numbers in a Range In JavaScript, bitwise operations provide a way to manipulate individual bits of numeric values. The bitwise OR operation (|) combines the bits of two operands, resulting in a new value where each bit is set if it is set in either operand. Understanding Bitwise ORThe bitwise OR operation takes two o
2 min read
JavaScript Program to Count Even and Odd Numbers in an Array In this article, we will write a program to count Even and Odd numbers in an array in JavaScript. Even numbers are those numbers that can be written in the form of 2n, while Odd numbers are those numbers that can be written in the form of 2n+1 form. For finding out the Even and Odd numbers in an arr
4 min read
JavaScript Program to Check if a Number has Bits in an Alternate Pattern JavaScript can be used to assess whether a given number follows an alternating pattern in its binary representation. By examining the binary digits of the number, one can determine if the sequence alternates between 0s and 1s, aiding in understanding the binary structure of the input. Examples: Inpu
2 min read
Swap all Odd & Even Bits using JavaScript The task is to swap all odd and even bits in a given decimal number. This involves manipulating the binary representation of the number by isolating odd and even bits, shifting them accordingly, and then combining them to produce the result. The goal is to exchange the positions of each pair of odd
4 min read
JavaScript Program to Find Neon Number in a Range A Neon number is a number where the sum of digits of the square of the number is equal to the number itself. For example, 9 is a Neon number because 9^2 = 81 and the sum of digits of 81 is 8 + 1 = 9, which is the number itself. We want to find all the Neon numbers within a given range. Below are the
3 min read