直到 N 的所有偶数的按位与
给定一个整数N ,任务是找到从 1 到 N 的所有偶数的按位和 (&)。
例子:
Input: 2
Output: 2
Input :10
Output : 0
Explanation: Bitwise and of 2, 4, 6, 8 and 10 are 0.
天真的方法:将结果初始化为 2。将循环从 4 迭代到 n(对于所有偶数)并通过查找按位和 (&) 来更新结果。
下面是该方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to return the bitwise &
// of all the even numbers upto N
int bitwiseAndTillN(int n)
{
// Initialize result as 2
int result = 2;
for (int i = 4; i <= n; i = i + 2) {
result = result & i;
}
return result;
}
// Driver code
int main()
{
int n = 2;
cout << bitwiseAndTillN(n);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to return the bitwise &
// of all the even numbers upto N
static int bitwiseAndTillN(int n)
{
// Initialize result as 2
int result = 2;
for (int i = 4; i <= n; i = i + 2)
{
result = result & i;
}
return result;
}
// Driver code
public static void main (String[] args)
{
int n = 2;
System.out.println(bitwiseAndTillN(n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach
# Function to return the bitwise &
# of all the even numbers upto N
def bitwiseAndTillN(n) :
# Initialize result as 2
result = 2;
for i in range(4, n + 1, 2) :
result = result & i;
return result;
# Driver code
if __name__ == "__main__" :
n = 2;
print(bitwiseAndTillN(n));
# This code is contributed by AnkitRai01
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to return the bitwise &
// of all the even numbers upto N
static int bitwiseAndTillN(int n)
{
// Initialize result as 2
int result = 2;
for (int i = 4; i <= n; i = i + 2)
{
result = result & i;
}
return result;
}
// Driver code
public static void Main()
{
int n = 2;
Console.WriteLine(bitwiseAndTillN(n));
}
}
// This code is contributed by AnkitRai01
Javascript
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to return the bitwise &
// of all the numbers upto N
int bitwiseAndTillN(int n)
{
if (n < 4)
return 2;
else
return 0;
}
int main()
{
int n = 2;
cout << bitwiseAndTillN(n);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to return the bitwise &
// of all the numbers upto N
static int bitwiseAndTillN(int n)
{
if (n < 4)
return 2;
else
return 0;
}
// Driver code
public static void main (String[] args)
{
int n = 2;
System.out.println(bitwiseAndTillN(n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach
# Function to return the bitwise &
# of all the numbers upto N
def bitwiseAndTillN( n):
if (n < 4):
return 2
else:
return 0
# Driver code
n = 2
print(bitwiseAndTillN(n))
# This code is contributed by ANKITKUMAR34
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to return the bitwise &
// of all the numbers upto N
static int bitwiseAndTillN(int n)
{
if (n < 4)
return 2;
else
return 0;
}
// Driver code
public static void Main()
{
int n = 2;
Console.WriteLine(bitwiseAndTillN(n));
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
2
有效的方法:有效的方法是对于 N 小于 4 返回 2,对于所有 N>=4 返回 0,因为 2 和 4 的按位与为 0,而 0 与任意数的按位与为 0。
下面是该方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to return the bitwise &
// of all the numbers upto N
int bitwiseAndTillN(int n)
{
if (n < 4)
return 2;
else
return 0;
}
int main()
{
int n = 2;
cout << bitwiseAndTillN(n);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to return the bitwise &
// of all the numbers upto N
static int bitwiseAndTillN(int n)
{
if (n < 4)
return 2;
else
return 0;
}
// Driver code
public static void main (String[] args)
{
int n = 2;
System.out.println(bitwiseAndTillN(n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach
# Function to return the bitwise &
# of all the numbers upto N
def bitwiseAndTillN( n):
if (n < 4):
return 2
else:
return 0
# Driver code
n = 2
print(bitwiseAndTillN(n))
# This code is contributed by ANKITKUMAR34
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to return the bitwise &
// of all the numbers upto N
static int bitwiseAndTillN(int n)
{
if (n < 4)
return 2;
else
return 0;
}
// Driver code
public static void Main()
{
int n = 2;
Console.WriteLine(bitwiseAndTillN(n));
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
2