Bitwise OR( | ) of all even number from 1 to N
Last Updated :
28 Dec, 2022
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 bitwise or ( | ).
Below is the implementation of the approach:
C++
// C++ implementation of the above approach
#include <iostream>
using namespace std;
// Function to return the bitwise OR
// of all the even numbers upto N
int bitwiseOrTillN(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 = 10;
cout << bitwiseOrTillN(n);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to return the bitwise OR
// of all the even numbers upto N
static int bitwiseOrTillN(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
static public void main (String args[])
{
int n = 10;
System.out.println(bitwiseOrTillN(n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python 3 implementation of the above approach
# Function to return the bitwise OR
# of all the even numbers upto N
def bitwiseOrTillN ( n ):
# Initialize result as 2
result = 2;
for i in range(4, n + 1, 2) :
result = result | i
return result
# Driver code
n = 10;
print(bitwiseOrTillN(n));
# This code is contributed by ANKITKUMAR34
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to return the bitwise OR
// of all the even numbers upto N
static int bitwiseOrTillN(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
static public void Main ()
{
int n = 10;
Console.WriteLine(bitwiseOrTillN(n));
}
}
// This code is contributed by AnkitRai01
JavaScript
<script>
// Javascript implementation of the above approach
// Function to return the bitwise OR
// of all the even numbers upto N
function bitwiseOrTillN(n)
{
// Initialize result as 2
var result = 2;
for(var i = 4; i <= n; i = i + 2)
{
result = result | i;
}
return result;
}
// Driver code
var n = 10;
document.write( bitwiseOrTillN(n));
// This code is contributed by noob2000
</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: Compute the total number of bits in N. In bitwise OR, the rightmost bit will be 0 and all other bits will be 1. Therefore, return pow(2, total no. of bits)-2. It will give the equivalent value in decimal of bitwise OR.
Below is the implementation of the approach:
C++
// C++ implementation of the above approach
#include <iostream>
#include <math.h>
using namespace std;
// Function to return the bitwise OR
// of all even numbers upto N
int bitwiseOrTillN(int n)
{
// For value less than 2
if (n < 2)
return 0;
// Count total number of bits in bitwise or
// all bits will be set except last bit
int bitCount = log2(n) + 1;
// Compute 2 to the power bitCount and subtract 2
return pow(2, bitCount) - 2;
}
// Driver code
int main()
{
int n = 10;
cout << bitwiseOrTillN(n);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to return the bitwise OR
// of all even numbers upto N
static int bitwiseOrTillN(int n)
{
// For value less than 2
if (n < 2)
return 0;
// Count total number of bits in bitwise or
// all bits will be set except last bit
int bitCount = (int)(Math.log(n)/Math.log(2)) + 1;
// Compute 2 to the power bitCount and subtract 2
return (int)Math.pow(2, bitCount) - 2;
}
// Driver code
public static void main (String[] args)
{
int n = 10;
System.out.println(bitwiseOrTillN(n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach
from math import log2
# Function to return the bitwise OR
# of all even numbers upto N
def bitwiseOrTillN(n) :
# For value less than 2
if (n < 2) :
return 0;
# Count total number of bits in bitwise or
# all bits will be set except last bit
bitCount = int(log2(n)) + 1;
# Compute 2 to the power bitCount and subtract 2
return pow(2, bitCount) - 2;
# Driver code
if __name__ == "__main__" :
n = 10;
print(bitwiseOrTillN(n));
# This code is contributed by AnkitRai01
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to return the bitwise OR
// of all even numbers upto N
static int bitwiseOrTillN(int n)
{
// For value less than 2
if (n < 2)
return 0;
// Count total number of bits in bitwise or
// all bits will be set except last bit
int bitCount = (int)(Math.Log(n)/Math.Log(2)) + 1;
// Compute 2 to the power bitCount and subtract 2
return (int)Math.Pow(2, bitCount) - 2;
}
// Driver code
public static void Main()
{
int n = 10;
Console.WriteLine(bitwiseOrTillN(n));
}
}
// This code is contributed by AnkitRai01
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the bitwise OR
// of all even numbers upto N
function bitwiseOrTillN(n)
{
// For value less than 2
if (n < 2)
return 0;
// Count total number of bits in bitwise or
// all bits will be set except last bit
var bitCount = parseInt(Math.log2(n) + 1);
// Compute 2 to the power bitCount and subtract 2
return Math.pow(2, bitCount) - 2;
}
// Driver code
var n = 10;
document.write( bitwiseOrTillN(n));
//This code is contributed by SoumikMondal
</script>
Time Complexity: O(log(log n), where n is the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Similar Reads
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
Bitwise AND of all even number up to N 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 t
5 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
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
Bitwise XOR of all odd numbers from a given range Given an integer N, the task is to find the Bitwise XOR of all odd numbers in the range [1, N]. Examples: Input: 11Output: 2Explanation: Bitwise XOR of all odd numbers up to 11 = 1 ^ 3 ^ 5 ^ 7 ^ 9 ^ 11 = 2. Input: 10Output: 9Explanation: Bitwise XOR of all odd numbers up to 10 = 1 ^ 3 ^ 5 ^ 7 ^ 9 =
9 min read
XOR of all even numbers from a given range Given two integers L and R, the task is to calculate Bitwise XOR of all even numbers in the range [L, R]. Examples: Example: Input: L = 10, R = 20 Output: 30 Explanation: Bitwise XOR = 10 ^ 12 ^ 14 ^ 16 ^ 18 ^ 20 = 30 Therefore, the required output is 30. Example: Input: L = 15, R = 23 Output: 0 Exp
6 min read