Check if bits in range L to R of two numbers are complement of each other or not
Last Updated :
21 Sep, 2022
Given two non-negative numbers a and b and two values l and r. The problem is to check whether all bits at corresponding positions in the range l to r in both the given numbers are complement of each other or not.
The bits are numbered from right to left, i.e., the least significant bit is considered to be at first position.
Examples:
Input: a = 10, b = 5
l = 1, r = 3
Output: Yes
(10)10 = (1010)2
(5)10 = (101)2 = (0101)2
All the bits in the range 1 to 3 are complement of each other.
Input: a = 21, b = 13
l = 2, r = 4
Output: No
(21)10 = (10101)2
(13)10 = (1101)2 = (1101)2
All the bits in the range 2 to 4 are not complement of each other.
Approach: Below are the steps to solve the problem
- Calculate xor_value = a ^ b.
- Check whether all the bits are set or not in the range l to r in xor_value. Refer this post.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool allBitsSetInTheGivenRange(unsigned int n,
unsigned int l,
unsigned int r)
{
int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
int new_num = n & num;
if (num == new_num)
return true ;
return false ;
}
bool bitsAreComplement(unsigned int a, unsigned int b,
unsigned int l, unsigned int r)
{
unsigned int xor_value = a ^ b;
return allBitsSetInTheGivenRange(xor_value, l, r);
}
int main()
{
unsigned int a = 10, b = 5;
unsigned int l = 1, r = 3;
if (bitsAreComplement(a, b, l, r))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static boolean allBitsSetInTheGivenRange( int n,
int l, int r)
{
int num = (( 1 << r) - 1 ) ^
(( 1 << (l - 1 )) - 1 );
int new_num = n & num;
if (num == new_num)
return true ;
return false ;
}
static boolean bitsAreComplement( int a, int b,
int l, int r)
{
int xor_value = a ^ b;
return allBitsSetInTheGivenRange(xor_value, l, r);
}
public static void main(String []args)
{
int a = 10 , b = 5 ;
int l = 1 , r = 3 ;
if (bitsAreComplement(a, b, l, r))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python 3
def allBitsSetInTheGivenRange(n, l, r):
num = (( 1 << r) - 1 ) ^ (( 1 << (l - 1 )) - 1 )
new_num = n & num
if (num = = new_num):
return True
return False
def bitsAreComplement(a, b, l, r):
xor_value = a ^ b
return allBitsSetInTheGivenRange(xor_value, l, r)
if __name__ = = "__main__" :
a = 10
b = 5
l = 1
r = 3
if (bitsAreComplement(a, b, l, r)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool allBitsSetInTheGivenRange( int n, int l,
int r)
{
int num = ((1 << r) - 1) ^
((1 << (l - 1)) - 1);
int new_num = n & num;
if (num == new_num)
return true ;
return false ;
}
static bool bitsAreComplement( int a, int b,
int l, int r)
{
int xor_value = a ^ b;
return allBitsSetInTheGivenRange(xor_value, l, r);
}
static public void Main ()
{
int a = 10, b = 5;
int l = 1, r = 3;
if (bitsAreComplement(a, b, l, r))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function allBitsSetInTheGivenRange( $n , $l , $r )
{
$num = ((1 << $r ) - 1) ^
((1 << ( $l - 1)) - 1);
$new_num = ( $n & $num );
if ( $num == $new_num )
return true;
return false;
}
function bitsAreComplement( $a , $b , $l , $r )
{
$xor_value = $a ^ $b ;
return allBitsSetInTheGivenRange( $xor_value , $l , $r );
}
$a = 10;
$b = 5;
$l = 1;
$r = 3;
if (bitsAreComplement( $a , $b , $l , $r ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function allBitsSetInTheGivenRange(n, l , r)
{
var num = ((1 << r) - 1) ^
((1 << (l - 1)) - 1);
var new_num = n & num;
if (num == new_num)
return true ;
return false ;
}
function bitsAreComplement(a , b,l , r)
{
var xor_value = a ^ b;
return allBitsSetInTheGivenRange(xor_value, l, r);
}
var a = 10, b = 5;
var l = 1, r = 3;
if (bitsAreComplement(a, b, l, r))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time complexity: O(1) as it is performing constant operations
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...