Bitwise AND of all the odd numbers from 1 to N - GeeksforGeeks

05 May, 2021
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++
Java
Python3
C#
Javascript
// 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 codeint main(){    int n = 10;     cout << bitwiseAndOdd(n);     return 0;}
// Java implementation of the approachclass 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 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 codeif __name__ == "__main__" :     n = 10;     print(bitwiseAndOdd(n)); # This code is contributed by AnkitRai01
// C# implementation of the approachusing 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
<script> // Javascript implementation of the approach // Function to return the bitwise AND// of all the odd integers from// the range [1, n]function bitwiseAndOdd(n){         // Initialize result to 1    var result = 1;     // Starting from 3, bitwise AND    // all the odd integers less    // than or equal to n    for(var i = 3; i <= n; i = i + 2)    {        result = (result & i);    }    return result;} // Driver codevar n = 10; document.write(bitwiseAndOdd(n)); // This code is contributed by noob2000 </script>
1
 
Time Complexity: O(n)
Auxiliary Space: O(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++
Java
Python 3
C#
Javascript
// 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 codeint main(){    int n = 10;     cout << bitwiseAndOdd(n);     return 0;}
// Java implementation of the approachclass 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 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 coden = 10print(bitwiseAndOdd(n)) # This code is contributed by ApurvaRaj
// C# implementation of the approachusing 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
<script>// Javascript implementation of the approach // Function to return the bitwise AND// of all the odd integers from// the range [1, n]function bitwiseAndOdd(n){    return 1;} // Driver codevar n = 10;document.write( bitwiseAndOdd(n)); //This code is contributed by SoumikMondal</script>
1
 
Time Complexity: O(1)
Auxiliary Space: O(1)
ApurvaRaj
ankthon
subhammahato348
noob2000
SoumikMondal
Bitwise-AND
Technical Scripter 2019
Bit Magic
Technical Scripter
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Check whether K-th bit is set or not
Program to find parity
Hamming code Implementation in C/C++
Write an Efficient Method to Check if a Number is Multiple of 3
Check for Integer Overflow
Builtin functions of GCC compiler
Swap bits in a given number
Implementation of Bit Stuffing and Bit Destuffing
Count number of bits to be flipped to convert A to B