Given 2 integers L, R as input, and find the bitwise OR of all the integers in the range [L, R] (both inclusive).
Example 1:
Input: L = 3, R = 8
Output: 15
Explanation: 3|4|5|6|7|8 = 15 
Ã¢â¬â¹Example 2:
Input: L = 12, R = 18
Output: 31
Explanation: 12|13|14|15|16|17|18 = 31
Your Task:  
You don't need to read input or print anything. Your task is to complete the function findBitwiseOR() which takes the L and R as inputs and returns the answer.
Expected Time Complexity: O(log R)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ L ≤ R ≤ 10^{12}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3
class Solution:
	def findBitwiseOR(self, L,R):
		# code here

Solutions:
class Solution:

	def findBitwiseOR(self, L, R):
		while L < R:
			L |= L + 1
		return R | L


class Solution:

	def findBitwiseOR(self, x, n):
		ans = max(n, x)
		flag = False
		for i in range(62, -1, -1):
			if x & 1 << i != n & 1 << i:
				flag = True
			if flag:
				ans = ans | 1 << i
		return ans


import math

class Solution:

	def FindpositionMSB(self, n):
		MSBval = -1
		while n:
			n = n >> 1
			MSBval += 1
		return MSBval

	def findBitwiseOR(self, L, R):
		result = 0
		msba = self.FindpositionMSB(L)
		msbb = self.FindpositionMSB(R)
		while msba == msbb:
			value = 1 << msba
			result += value
			L -= value
			R -= value
			msba = self.FindpositionMSB(L)
			msbb = self.FindpositionMSB(R)
		msba = max(msba, msbb)
		i = msba
		while i >= 0:
			res_val = 1 << i
			result += res_val
			i -= 1
		return result


class Solution:

	def findBitwiseOR(self, L, R):

		def MSBPosition(N):
			msb_p = -1
			while N:
				N = N >> 1
				msb_p += 1
			return msb_p
		res = 0
		msb_p1 = MSBPosition(L)
		msb_p2 = MSBPosition(R)
		while msb_p1 == msb_p2:
			res_val = 1 << msb_p1
			res += res_val
			L -= res_val
			R -= res_val
			msb_p1 = MSBPosition(L)
			msb_p2 = MSBPosition(R)
		msb_p1 = max(msb_p1, msb_p2)
		for i in range(msb_p1, -1, -1):
			res_val = 1 << i
			res += res_val
		return res


class Solution:

	def findBitwiseOR(self, L, R):
		num_count = R - L
		tmp = R
		res = 0
		mask = 1
		while tmp > 0:
			if tmp & 1:
				res = res | mask
			elif R % mask + 1 <= num_count:
				res = res | mask
			mask = mask << 1
			tmp = tmp >> 1
		return res


class Solution:

	def findBitwiseOR(self, l, r):
		if l == r:
			return l
		x = bin(l)[2:]
		y = bin(r)[2:]
		n = len(x)
		m = len(y)
		if n != m:
			return int('1' * m, 2)
		c = 0
		while l != r:
			l = l >> 1
			r = r >> 1
			c += 1
		x = bin(l)[2:] + '1' * c
		return int(x, 2)