Range OR

Tags : geeksforgeeks, math, cpp, easy

You are given an integer N. find the bitwise OR of all the numbers in the range [1, N].

Your Task:

The task is to complete the function rangeOR() which takes an integer N as input parameter and returns the bitwise OR of all the numbers in the range [1, N].

Examples #

Example 1:

Input: 
N = 3
Output:
3
Explanation:
1 | 2 | 3 = 3

Example 2:

Input:
N = 1
Output:
1
Explanation:
1 = 1

Constraints #

Solutions #


class Solution{
	public:
	int rangeOR(int n)
	{
		//code here
		int temp = 1, res = n;
		while( temp <= n) {
		    res = res | temp;
		    temp <<= 1;
		    n |= (n >> 1);
		    
		}
		return res;
		
	}
};