Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
Last Updated :
30 Apr, 2021
Given an integer N, the task is to find the largest number M, where (M < N), such that N(XOR)M is equal to N(OR)M i.e. (N ^ M) = (N | M).
Examples:
Input: N = 5
Output: 2
5 ^ 4 = 1 and 5 | 4 = 5. Therefore, XOR and OR between them are not equal.
5 ^ 3 = 6 and 5 | 3 = 7. Therefore, XOR and OR between them are not equal.
5 ^ 2 = 7 and 5 | 2 = 7. Therefore, XOR and OR between them are equal.
Input: N = 14
Output: 1
Approach:
To get the required number M, traverse all the bits of N from its Least Significant Bit (LSB) to Most Significant Bit (MSB). Two cases arise here:
- If the ith bit of N is 1 then:
- If the ith bit of M is set to 1, then N^M will not be equal to N|M as (1^1 = 0) and (1|1 = 1).
- If the ith bit is set of M to 0, then N^M will be equal to N|M as (1^0 = 1) and (1|0 = 1).
- So if the ith bit of N is 1, set the ith bit of M to 0.
- If the ith bit of N is 0 then:
- If the ith bit of M is set to 1, then N^M will be equal to N|M as (0^1 = 1) and (0|1 = 1).
- If we set the ith bit of M to 0, then N^M will be equal to N|M as (0^0 = 0) and (0|0 = 0).
- So, if the ith bit of M is set to either 0 or 1, N^M will always be equal to N|M.
- As the largest value of M which is less than N has to be found out, always set the ith bit of M to 1.
Illustration:
- N = 5
- 32-bit representation of 5 = 00000000000000000000000000000101
- LSB index of 5 = 31
- MSB index of 5 = 29
- Traversing from LSB to MSB i.e. from 31 to 29:
- For index 31, N[31] = 1. So M[31] should be set to 0.
- For index 30, N[30] = 0. So M[30] should be set to 1.
- For index 29, N[29] = 1. So M[29] should be set to 0.
- Thus the 32-bit representation of M is 00000000000000000000000000000010, which is equal to 2 in decimal representation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int equalXORandOR( int n)
{
int m = 0;
int MSB = ( int )log2(n);
for ( int i = 0; i <= MSB; i++) {
if (!(n & (1 << i))) {
m += (1 << i);
}
}
return m;
}
int main()
{
int n = 14;
cout << equalXORandOR(n);
return 0;
}
|
Java
class GFG{
static int equalXORandOR( int n)
{
int m = 0 ;
int MSB = ( int )Math.log(n);
for ( int i = 0 ; i <= MSB; i++)
{
if ((n & ( 1 << i)) <= 0 )
{
m += ( 1 << i);
}
}
return m;
}
public static void main(String[] args)
{
int n = 14 ;
System.out.print(equalXORandOR(n));
}
}
|
Python3
from math import log2
def equalXORandOR(n):
m = 0
MSB = int (log2(n))
for i in range (MSB + 1 ):
if ( not (n & ( 1 << i))):
m + = ( 1 << i)
return m
n = 14
print (equalXORandOR(n))
|
C#
using System;
class GFG{
static int equalXORandOR( int n)
{
int m = 0;
int MSB = ( int )Math.Log(n);
for ( int i = 0; i <= MSB; i++)
{
if ((n & (1 << i)) <= 0)
{
m += (1 << i);
}
}
return m;
}
public static void Main(String[] args)
{
int n = 14;
Console.Write(equalXORandOR(n));
}
}
|
Javascript
<script>
function equalXORandOR(n) {
var m = 0;
var MSB = parseInt( Math.log(n));
for (i = 0; i <= MSB; i++) {
if ((n & (1 << i)) <= 0) {
m += (1 << i);
}
}
return m;
}
var n = 14;
document.write(equalXORandOR(n));
</script>
|
Time Complexity: O(log2 N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...