# Bitwise AND of all the odd numbers from 1 to N

Given an integer N, the task is to find the bitwise AND (&) of all the odd integers from the range [1, N].

Examples:

Input: N = 7
Output: 1
(1 & 3 & 5 & 7) = 1

Input: N = 1
Output: 1

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

Naive approach: Starting from 1, bitwise AND all the odd numbers ≤ N.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the bitwise AND ` `// of all the odd integers from ` `// the range [1, n] ` `int` `bitwiseAndOdd(``int` `n) ` `{ ` `    ``// Initialize result to 1 ` `    ``int` `result = 1; ` ` `  `    ``// Starting from 3, bitwise AND ` `    ``// all the odd integers less ` `    ``// than or equal to n ` `    ``for` `(``int` `i = 3; i <= n; i = i + 2) { ` `        ``result = (result & i); ` `    ``} ` `    ``return` `result; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 10; ` ` `  `    ``cout << bitwiseAndOdd(n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG ` `{ ` `     `  `    ``// Function to return the bitwise AND ` `    ``// of all the odd integers from ` `    ``// the range [1, n] ` `    ``static` `int` `bitwiseAndOdd(``int` `n) ` `    ``{ ` `        ``// Initialize result to 1 ` `        ``int` `result = ``1``; ` `     `  `        ``// Starting from 3, bitwise AND ` `        ``// all the odd integers less ` `        ``// than or equal to n ` `        ``for` `(``int` `i = ``3``; i <= n; i = i + ``2``) ` `        ``{ ` `            ``result = (result & i); ` `        ``} ` `        ``return` `result; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `         `  `        ``int` `n = ``10``; ` `         `  `        ``System.out.println(bitwiseAndOdd(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to return the bitwise AND  ` `# of all the odd integers from  ` `# the range [1, n]  ` `def` `bitwiseAndOdd(n) :  ` ` `  `    ``# Initialize result to 1  ` `    ``result ``=` `1``;  ` ` `  `    ``# Starting from 3, bitwise AND  ` `    ``# all the odd integers less  ` `    ``# than or equal to n  ` `    ``for` `i ``in` `range``(``3``, n ``+` `1``, ``2``) : ` `        ``result ``=` `(result & i);  ` ` `  `    ``return` `result;  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``n ``=` `10``;  ` ` `  `    ``print``(bitwiseAndOdd(n));  ` ` `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `    ``// Function to return the bitwise AND ` `    ``// of all the odd integers from ` `    ``// the range [1, n] ` `    ``static` `int` `bitwiseAndOdd(``int` `n) ` `    ``{ ` `        ``// Initialize result to 1 ` `        ``int` `result = 1; ` `     `  `        ``// Starting from 3, bitwise AND ` `        ``// all the odd integers less ` `        ``// than or equal to n ` `        ``for` `(``int` `i = 3; i <= n; i = i + 2) ` `        ``{ ` `            ``result = (result & i); ` `        ``} ` `        ``return` `result; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `         `  `        ``int` `n = 10; ` `        ``Console.WriteLine(bitwiseAndOdd(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by AnkitRai01 `

Output:

```1
```

Efficient approach: Bitwise AND with 1 will always give 1 as the result if the integer has a one at the least significant bit (all the odd integers in this case). So, the result will be 1 in all the cases.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the bitwise AND ` `// of all the odd integers from ` `// the range [1, n] ` `int` `bitwiseAndOdd(``int` `n) ` `{ ` `    ``return` `1; ` `} ` ` `  `// Driver code ` `int` `main()  ` `{ ` `    ``int` `n = 10; ` ` `  `    ``cout << bitwiseAndOdd(n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `class` `GFG  ` `{ ` ` `  `    ``// Function to return the bitwise AND  ` `    ``// of all the odd integers from  ` `    ``// the range [1, n]  ` `    ``static` `int` `bitwiseAndOdd(``int` `n)  ` `    ``{  ` `        ``return` `1``;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{  ` `        ``int` `n = ``10``;  ` `     `  `        ``System.out.println(bitwiseAndOdd(n));  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python 3

 `# Python 3 implementation of the approach ` ` `  `# Function to return the bitwise AND ` `# of all the odd integers from ` `# the range [1, n] ` `def` `bitwiseAndOdd(n): ` `    ``return` `1` ` `  `# Driver code ` `n ``=` `10` `print``(bitwiseAndOdd(n)) ` ` `  `# This code is contributed by ApurvaRaj `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` ` `  `    ``// Function to return the bitwise AND  ` `    ``// of all the odd integers from  ` `    ``// the range [1, n]  ` `    ``static` `int` `bitwiseAndOdd(``int` `n)  ` `    ``{  ` `        ``return` `1;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``int` `n = 10;  ` `     `  `        ``Console.WriteLine(bitwiseAndOdd(n));  ` `    ``}  ` `}  ` ` `  `// This code is contributed by AnkitRai01  `

Output:

```1
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : ApurvaRaj, AnkitRai01

Article Tags :
Practice Tags :

2

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