📜  从1到N的所有奇数的按位与

📅  最后修改于: 2021-05-25 04:32:25             🧑  作者: Mango

给定一个整数N ,任务是从范围[1,N]中查找所有奇数整数的按位与(&)。

例子:

天真的方法:1开始,按位与所有奇数≤N

下面是上述方法的实现:

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


Javascript


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


Javascript


输出:
1

时间复杂度: O(n)

辅助空间: O(1)

有效的方法:如果整数的最低有效位为1(在这种情况下为所有奇数整数),则与1进行按位与运算将始终得到1。因此,在所有情况下,结果均为1。

下面是上述方法的实现:

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

Java脚本


输出:
1

时间复杂度: O(1)

辅助空间: O(1)