# Bitwise and (or &) of a range

Given two non-negative long integers, x and y given x <= y, the task is to find bit-wise and of all integers from x and y, i.e., we need to compute value of x & (x+1) & … & (y-1) & y.7

Examples:

```Input  : x = 12, y = 15
Output : 12
12 & 13 & 14 & 15 = 12

Input  : x = 10, y = 20
Output : 0
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution is to traverse all numbers from x to y and do bit-wise and of all numbers in range.

An efficient solution is to follow following steps.
1) Find position of Most Significant Bit (MSB) in both numbers.
2) If positions of MSB are different, then result is 0.
3) If positions are same. Let positions be msb_p.
……a) We add 2msb_p to result.
……b) We subtract 2msb_p from x and y,
……c) Repeat steps 1, 2 and 3 for new values of x and y.

```Example 1 :
x = 10, y = 20
Result is initially 0.
Position of MSB in x = 3
Position of MSB in y = 4
Since positions are different, return result.

Example 2 :
x = 17, y = 19
Result is initially 0.
Position of MSB in x = 4
Position of MSB in y = 4
Since positions are same, we compute 24.

Result becomes 16.

We subtract this value from x and y.
New value of x  = x - 24  = 17 - 16 = 1
New value of y  = y - 24  = 19 - 16 = 3

Position of MSB in new x = 1
Position of MSB in new y = 2
Since positions are different, we return result.

```

## C++

 `// An efficient C++ program to find bit-wise & of all ` `// numbers from x to y. ` `#include ` `using` `namespace` `std; ` `typedef` `long` `long` `int` `ll; ` ` `  `// Find position of MSB in n. For example if n = 17, ` `// then position of MSB is 4. If n = 7, value of MSB ` `// is 3 ` `int` `msbPos(ll n) ` `{ ` `    ``int` `msb_p = -1; ` `    ``while` `(n) ` `    ``{ ` `        ``n = n>>1; ` `        ``msb_p++; ` `    ``} ` `    ``return` `msb_p; ` `} ` ` `  `// Function to find Bit-wise & of all numbers from x ` `// to y. ` `ll andOperator(ll x, ll y) ` `{ ` `    ``ll res = 0; ``// Initialize result ` ` `  `    ``while` `(x && y) ` `    ``{ ` `        ``// Find positions of MSB in x and y ` `        ``int` `msb_p1 = msbPos(x); ` `        ``int` `msb_p2 = msbPos(y); ` ` `  `        ``// If positions are not same, return ` `        ``if` `(msb_p1 != msb_p2) ` `            ``break``; ` ` `  `        ``// Add 2^msb_p1 to result ` `        ``ll msb_val =  (1 << msb_p1); ` `        ``res = res + msb_val; ` ` `  `        ``// subtract 2^msb_p1 from x and y. ` `        ``x = x - msb_val; ` `        ``y = y - msb_val; ` `    ``} ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``ll x = 10, y = 15; ` `    ``cout << andOperator(x, y); ` `    ``return` `0; ` `} `

## Java

 `// An efficient Java program to find bit-wise  ` `// & of all numbers from x to y.  ` `class` `GFG {  ` `     `  `    ``// Find position of MSB in n. For example  ` `    ``// if n = 17, then position of MSB is 4.  ` `    ``// If n = 7, value of MSB is 3  ` `    ``static` `int` `msbPos(``long` `n)  ` `    ``{  ` `         `  `        ``int` `msb_p = -``1``;  ` `        ``while` `(n > ``0``) {  ` `            ``n = n >> ``1``;  ` `            ``msb_p++;  ` `        ``}  ` `         `  `        ``return` `msb_p;  ` `    ``}  ` ` `  `    ``// Function to find Bit-wise & of all  ` `    ``// numbers from x to y.  ` `    ``static` `long` `andOperator(``long` `x, ``long` `y)  ` `    ``{  ` `         `  `        ``long` `res = ``0``; ``// Initialize result  ` ` `  `        ``while` `(x > ``0` `&& y > ``0``) {  ` `             `  `            ``// Find positions of MSB in x and y  ` `            ``int` `msb_p1 = msbPos(x);  ` `            ``int` `msb_p2 = msbPos(y);  ` ` `  `            ``// If positions are not same, return  ` `            ``if` `(msb_p1 != msb_p2)  ` `                ``break``;  ` ` `  `            ``// Add 2^msb_p1 to result  ` `            ``long` `msb_val = (``1` `<< msb_p1);  ` `            ``res = res + msb_val;  ` ` `  `            ``// subtract 2^msb_p1 from x and y.  ` `            ``x = x - msb_val;  ` `            ``y = y - msb_val;  ` `        ``}  ` ` `  `        ``return` `res;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{  ` `         `  `        ``long` `x = ``10``, y = ``15``;  ` `         `  `        ``System.out.print(andOperator(x, y));  ` `    ``}  ` `}  ` ` `  `// This code is contributed by Anant Agarwal.  `

## Python3

 `# An efficient Python program to find  ` `# bit-wise & of all numbers from x to y.  ` ` `  `# Find position of MSB in n. For example  ` `# if n = 17, then position of MSB is 4.  ` `# If n = 7, value of MSB is 3  ` `def` `msbPos(n):  ` ` `  `    ``msb_p ``=` `-``1` `    ``while` `(n > ``0``):  ` `     `  `        ``n ``=` `n >> ``1` `        ``msb_p ``+``=` `1` `     `  `    ``return` `msb_p  ` ` `  `# Function to find Bit-wise & of  ` `# all numbers from x to y.  ` `def` `andOperator(x, y):  ` ` `  `    ``res ``=` `0` `# Initialize result  ` ` `  `    ``while` `(x > ``0` `and` `y > ``0``):  ` `     `  `        ``# Find positions of MSB in x and y  ` `        ``msb_p1 ``=` `msbPos(x)  ` `        ``msb_p2 ``=` `msbPos(y)  ` ` `  `        ``# If positions are not same, return  ` `        ``if` `(msb_p1 !``=` `msb_p2):  ` `            ``break` ` `  `        ``# Add 2^msb_p1 to result  ` `        ``msb_val ``=` `(``1` `<< msb_p1)  ` `        ``res ``=` `res ``+` `msb_val  ` ` `  `        ``# subtract 2^msb_p1 from x and y.  ` `        ``x ``=` `x ``-` `msb_val  ` `        ``y ``=` `y ``-` `msb_val  ` ` `  `    ``return` `res  ` `     `  `# Driver code  ` `x, y ``=` `10``, ``15` `print``(andOperator(x, y))  ` ` `  `# This code is contributed by Anant Agarwal.  `

## C#

 `// An efficient C# program to find bit-wise & of all  ` `// numbers from x to y.  ` `using` `System;  ` ` `  `class` `GFG  ` `{  ` `    ``// Find position of MSB in n.  ` `    ``// For example if n = 17,  ` `    ``// then position of MSB is 4.  ` `    ``// If n = 7, value of MSB  ` `    ``// is 3  ` `    ``static` `int` `msbPos(``long` `n)  ` `    ``{  ` `        ``int` `msb_p = -1;  ` `        ``while` `(n > 0)  ` `        ``{  ` `            ``n = n >> 1;  ` `            ``msb_p++;  ` `        ``}  ` `        ``return` `msb_p;  ` `    ``}  ` `     `  `    ``// Function to find Bit-wise  ` `    ``// & of all numbers from x  ` `    ``// to y.  ` `    ``static` `long` `andOperator(``long` `x, ``long` `y)  ` `    ``{  ` `        ``// Initialize result  ` `        ``long` `res = 0;  ` `     `  `        ``while` `(x > 0 && y > 0)  ` `        ``{  ` `            ``// Find positions of MSB in x and y  ` `            ``int` `msb_p1 = msbPos(x);  ` `            ``int` `msb_p2 = msbPos(y);  ` `     `  `            ``// If positions are not same, return  ` `            ``if` `(msb_p1 != msb_p2)  ` `                ``break``;  ` `     `  `            ``// Add 2^msb_p1 to result  ` `            ``long` `msb_val = (1 << msb_p1);  ` `            ``res = res + msb_val;  ` `     `  `            ``// subtract 2^msb_p1 from x and y.  ` `            ``x = x - msb_val;  ` `            ``y = y - msb_val;  ` `        ``}  ` `     `  `        ``return` `res;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``long` `x = 10, y = 15;  ` `        ``Console.WriteLine(andOperator(x, y));  ` `    ``}  ` `}  ` ` `  `// This code is contributed by Anant Agarwal.  `

## PHP

 ` 0) ` `    ``{ ` `        ``\$n` `= ``\$n` `>> 1; ` `        ``\$msb_p``++; ` `    ``} ` `    ``return` `\$msb_p``; ` `} ` ` `  `// Function to find Bit-wise &  ` `// of all numbers from x to y. ` `function` `andOperator(``\$x``, ``\$y``) ` `{ ` `    ``\$res` `= 0; ``// Initialize result ` ` `  `    ``while` `(``\$x` `> 0 && ``\$y` `> 0) ` `    ``{ ` `        ``// Find positions of  ` `        ``// MSB in x and y ` `        ``\$msb_p1` `= msbPos(``\$x``); ` `        ``\$msb_p2` `= msbPos(``\$y``); ` ` `  `        ``// If positions are not ` `        ``// same, return ` `        ``if` `(``\$msb_p1` `!= ``\$msb_p2``) ` `            ``break``; ` ` `  `        ``// Add 2^msb_p1 to result ` `        ``\$msb_val` `= (1 << ``\$msb_p1``); ` `        ``\$res` `= ``\$res` `+ ``\$msb_val``; ` ` `  `        ``// subtract 2^msb_p1  ` `        ``// from x and y. ` `        ``\$x` `= ``\$x` `- ``\$msb_val``; ` `        ``\$y` `= ``\$y` `- ``\$msb_val``; ` `    ``} ` ` `  `    ``return` `\$res``; ` `} ` ` `  `// Driver code ` `\$x` `= 10; ` `\$y` `= 15; ` `echo` `andOperator(``\$x``, ``\$y``); ` ` `  `// This code is contributed  ` `// by ihritik ` `?> `

Output:

```8
```

More efficient solution

1. Flip the LSB of b.
2. And check if the new number is in range(a < number < b) or not
• if the number greater than 'a' again flip lsb
• if it is not then that's the answer

NOTE: We are not considering inclusive range so if the number is 'a' we shouldn't flip anymore i.e.'a' will be the answer.

```Example 1 :
x = 10, y = 20
y = 10100
Flip LSB of y New Number = 10000 i.e. 16
16 is in range so Again flip : 00000 i.e. 0 and 0 is not in range so 0 is the answer.

Example 2 :
x = 17, y = 19
y = 10011
Flip LSB of y so New Number = 10010 i.e. 18
18 is in range so Again flip : 10000 i.e. 16 and 16 is 'a' so Stop , answer is 16.
```

## C++

 `// An efficient C++ program to find bit-wise & of all ` `// numbers from x to y. ` `#include ` `#define ll long long ` ` `  `using` `namespace` `std; ` `ll andOperator(ll a, ll b){ ` `while``(a < b){ ` `    ``// -b is the 2's complement of b when do bitwise or with b  ` `    ``//we get LSB and we subtract that from b ` `    ``b -= (b & -b); ` `} ` `return` `b; ` `} ` `// Driver code ` `int` `main(){ ` `    ``ll a, b; ` `    ``a = 10; b = 15; ` `    ``cout << andOperator(a, b); ` `} `

## Java

 `// An efficient Java program to find bit-wise & of all  ` `// numbers from x to y. ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``static` `long` `andOperator(``long` `a,``long` `b) ` `    ``{  ` `        ``while``(a < b) ` `        ``{  ` `            ``// -b is the 2's complement of b when do bitwise or with b  ` `            ``//we get LSB and we subtract that from b  ` `            ``b -= (b & -b);  ` `        ``}  ` `        ``return` `b;  ` `    ``}  ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `        ``long` `a, b;  ` `        ``a = ``10``; b = ``15``;  ` `        ``System.out.println(andOperator(a, b)); ` `    ``} ` `} ` ` `  `// This code is contributed by apurva raj `

## Python3

 `# An efficient Python3 program to find  ` `# bit-wise & of all numbers from x to y.  ` `def` `andOperator(a, b): ` `    ``while``(a < b): ` `         `  `        ``# -b is the 2's complement of b  ` `        ``# when do bitwise or with b we ` `        ``# get LSB and we subtract that from b  ` `        ``b ``-``=` `(b & ``-``b)  ` `    ``return` `b  ` ` `  `# Driver code  ` `a, b ``=` `10``, ``15` `print``(andOperator(a, b)) ` ` `  `# This code is contributed by  ` `# Shubham Singh (SHUBHAMSINGH10) `

## C#

 `// An efficient C# program to find bit-wise & of all  ` `// numbers from x to y.  ` `using` `System;  ` ` `  `class` `GFG  ` `{  ` `    ``static` `long` `andOperator(``long` `a, ``long` `b) ` `    ``{  ` `        ``while``(a < b) ` `        ``{  ` `            ``// -b is the 2's complement of b  ` `            ``// when do bitwise or with b  ` `            ``// we get LSB and we subtract that from b  ` `            ``b -= (b & -b);  ` `        ``}  ` `        ``return` `b;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``long` `a, b;  ` `        ``a = 10; b = 15;  ` `        ``Console.WriteLine(andOperator(a, b));  ` `    ``}  ` `} ` ` `  `// This code is contributed by SHUBHAMSINGH10 `

Output:

```8
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

9

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.