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

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



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


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