Bitwise AND of all even number up to N
Last Updated :
28 Dec, 2022
Given an integer N, the task is to find bitwise and (&) of all even numbers from 1 to N.
Examples:
Input: 2
Output: 2
Input :10
Output : 0
Explanation: Bitwise and of 2, 4, 6, 8 and 10 are 0.
Naive approach: Initialize result as 2. Iterate the loop from 4 to n (for all even numbers) and update the result by finding bitwise and (&).
Below is the implementation of the approach:
C++
// C++ implementation of the above approach
#include <iostream>
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
<script>
// Javascript implementation of the above approach
// Function to return the bitwise &
// of all the even numbers upto N
function bitwiseAndTillN(n) {
// Initialize result as 2
let result = 2;
for (let i = 4; i <= n; i = i + 2) {
result = result & i;
}
return result;
}
// Driver code
let n = 2;
document.write(bitwiseAndTillN(n));
</script>
Time Complexity: O(n), where n is the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Efficient approach: Efficient approach is to return 2 for N less than 4 and return 0 for all N>=4 because bitwise and of 2 and 4 is 0 and bitwise and of 0 with any number is 0.
Below is the implementation of the approach:
C++
// C++ implementation of the above approach
#include <iostream>
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
<script>
// JavaScript implementation of the above approach
// Function to return the bitwise &
// of all the numbers upto N
function bitwiseAndTillN(n)
{
if (n < 4)
return 2;
else
return 0;
}
// driver code
let n = 2;
document.write (bitwiseAndTillN(n));
// this code is contributed by shivanisinghss2110
</script>
Time complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Bitwise OR( | ) of all even number from 1 to N Given a number N, the task is to find the bitwise OR( | ) of all even numbers from 1 to N. Examples: Input: 2 Output: 2 Input: 10 Output: 14 Explanation: 2 | 4 | 6 | 8 | 10 = 14 Naive Approach: Initialize the result as 2.Iterate the loop from 4 to n (for all even number) and update result by finding
6 min read
Set all even bits of a number Given a number, the task is to set all even bits of a number. Positions of bits are counted from LSB (least significant bit) to MSB (Most significant bit). The position of LSB is considered as 1. Examples : Input : 20 Output : 30 Binary representation of 20 is 10100. After setting even bits, we get
14 min read
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
5 min read
Toggle all even bits of a number Given a number, the task is to Toggle all even bit of a numberExamples: Input : 10 Output : 0 binary representation 1 0 1 0 after toggle 0 0 0 0 Input : 20 Output : 30 binary representation 1 0 1 0 0 after toggle 1 1 1 1 0 1. First generate a number that contains even position bits. 2. Take XOR with
8 min read
Change all even bits in a number to 0 Given a number, change all bits at even positions to 0. Examples: Input : 30 Output : 10 Binary representation of 11110. Bits at Even positions are highlighted. After making all of them 0, we get 01010 Input : 10 Output : 10Recommended PracticeChange all even bits in a number to 0Try It! Method 1 (B
6 min read
Check whether bitwise AND of N numbers is Even or Odd Given an array arr[] containing N numbers. The task is to check whether the bitwise-AND of the given N numbers is even or odd.Examples: Input: arr[] = { 2, 12, 20, 36, 38 } Output: Even Input: arr[] = { 3, 9, 17, 13, 15 } Output: Odd A Simple Solution is to first find the AND of the given N numbers,
7 min read