codeparrot/xlcost-text-to-code huggingface dataset:
Toggle first and last bits of a number | CPP program to toggle first and last bits of a number ; Returns a number which has same bit count as n and has only first and last bits as set . ; set all the bit of the number ; Adding one to n now unsets all bits and moves MSB to one place . Now we shift the number by 1 and add 1. ; if number is 1 ; take XOR with first and last set bit number ; Driver code
#include <iostream> NEW_LINE using namespace std ; int takeLandFsetbits ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( ( n + 1 ) >> 1 ) + 1 ; } int toggleFandLbits ( int n ) { if ( n == 1 ) return 0 ; return n ^ takeLandFsetbits ( n ) ; } int main ( ) { int n = 10 ; cout << toggleFandLbits ( n ) ; return 0 ; }
Odious number | C / C ++ program to check if a number is Odious Number or not ; Function to get no of set bits in binary representation of passed binary no . Please refer below for details of this function : https : www . geeksforgeeks . org / count - set - bits - in - an - integer / ; Check if number is odious or not ; Driver Code
#include <iostream> NEW_LINE using namespace std ; #include <math.h> NEW_LINE int countSetBits ( int n ) { unsigned int count = 0 ; while ( n ) { n &= ( n - 1 ) ; count ++ ; } return count ; } int checkOdious ( int n ) { return ( countSetBits ( n ) % 2 == 1 ) ; } int main ( ) { int num = 32 ; if ( checkOdious ( num ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Set the Left most unset bit | C ++ program to set the leftmost unset bit ; set left most unset bit ; if number contain all 1 then return n ; Find position of leftmost unset bit . ; if temp L . S . B is zero then unset bit pos is change ; return OR of number and unset bit pos ; Driver Function
#include <iostream> NEW_LINE using namespace std ; int setleftmostunsetbit ( int n ) { if ( ( n & ( n + 1 ) ) == 0 ) return n ; int pos = 0 ; for ( int temp = n , count = 0 ; temp > 0 ; temp >>= 1 , count ++ ) if ( ( temp & 1 ) == 0 ) pos = count ; return ( n | ( 1 << ( pos ) ) ) ; } int main ( ) { int n = 10 ; cout << setleftmostunsetbit ( n ) ; return 0 ; }
Maximum XOR using K numbers from 1 to n | CPP program to find max xor sum of 1 to n using atmost k numbers ; To return max xor sum of 1 to n using at most k numbers ; If k is 1 then maximum possible sum is n ; Finding number greater than or equal to n with most significant bit same as n . For example , if n is 4 , result is 7. If n is 5 or 6 , result is 7 ; Return res - 1 which denotes a number with all bits set to 1 ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxXorSum ( int n , int k ) { if ( k == 1 ) return n ; int res = 1 ; while ( res <= n ) res <<= 1 ; return res - 1 ; } int main ( ) { int n = 4 , k = 3 ; cout << maxXorSum ( n , k ) ; return 0 ; }
Increment a number by one by manipulating the bits | C ++ implementation to increment a number by one by manipulating the bits ; function to find the position of rightmost set bit ; function to toggle the last m bits ; calculating a number ' num ' having ' m ' bits and all are set ; toggle the last m bits and return the number ; function to increment a number by one by manipulating the bits ; get position of rightmost unset bit if all bits of ' n ' are set , then the bit left to the MSB is the rightmost unset bit ; kth bit of n is being set by this operation ; from the right toggle all the bits before the k - th bit ; required number ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getPosOfRightmostSetBit ( int n ) { return log2 ( n & - n ) ; } unsigned int toggleLastKBits ( unsigned int n , unsigned int k ) { unsigned int num = ( 1 << k ) - 1 ; return ( n ^ num ) ; } unsigned int incrementByOne ( unsigned int n ) { int k = getPosOfRightmostSetBit ( ~ n ) ; n = ( ( 1 << k ) n ) ; if ( k != 0 ) n = toggleLastKBits ( n , k ) ; return n ; } int main ( ) { unsigned int n = 15 ; cout << incrementByOne ( n ) ; return 0 ; }
XNOR of two numbers | CPP program to find XNOR of two numbers ; log ( n ) solution ; Make sure a is larger ; for last bit of a ; for last bit of b ; counter for count bit and set bit in xnornum ; to make new xnor number ; for set bits in new xnor number ; get last bit of a ; get last bit of b ; Check if current two bits are same ; counter for count bit ; Driver code
#include <iostream> NEW_LINE using namespace std ; int xnor ( int a , int b ) { if ( a < b ) swap ( a , b ) ; if ( a == 0 && b == 0 ) return 1 ; int a_rem = 0 ; int b_rem = 0 ; int count = 0 ; int xnornum = 0 ; while ( a ) { a_rem = a & 1 ; b_rem = b & 1 ; if ( a_rem == b_rem ) xnornum |= ( 1 << count ) ; count ++ ; a = a >> 1 ; b = b >> 1 ; } return xnornum ; } int main ( ) { int a = 10 , b = 50 ; cout << xnor ( a , b ) ; return 0 ; }
XNOR of two numbers | CPP program to find XNOR of two numbers . ; Please refer below post for details of this function https : www . geeksforgeeks . org / toggle - bits - significant - bit / ; Make a copy of n as we are going to change it . ; Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001 ; This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111 ; Returns XNOR of num1 and num2 ; if num2 is greater then we swap this number in num1 ; Driver code
#include <iostream> NEW_LINE using namespace std ; int togglebit ( int n ) { if ( n == 0 ) return 1 ; int i = n ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return i ^ n ; } int XNOR ( int num1 , int num2 ) { if ( num1 < num2 ) swap ( num1 , num2 ) ; num1 = togglebit ( num1 ) ; return num1 ^ num2 ; } int main ( ) { int num1 = 10 , num2 = 20 ; cout << XNOR ( num1 , num2 ) ; return 0 ; }
Maximum OR sum of sub | CPP program to find maximum OR sum ; function to find maximum OR sum ; OR sum of all the elements in both arrays ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MaximumSum ( int a [ ] , int b [ ] , int n ) { int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum1 |= a [ i ] ; sum2 |= b [ i ] ; } cout << sum1 + sum2 << endl ; } int main ( ) { int A [ ] = { 1 , 2 , 4 , 3 , 2 } ; int B [ ] = { 2 , 3 , 3 , 12 , 1 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; MaximumSum ( A , B , n ) ; return 0 ; }
Position of rightmost bit with first carry in sum of two binary | C ++ implementation to find the position of rightmost bit where a carry is generated first ; function to find the position of rightmost set bit in ' n ' ; function to find the position of rightmost bit where a carry is generated first ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef unsigned long long int ull ; unsigned int posOfRightmostSetBit ( ull n ) { return log2 ( n & - n ) + 1 ; } unsigned int posOfCarryBit ( ull a , ull b ) { return posOfRightmostSetBit ( a & b ) ; } int main ( ) { ull a = 10 , b = 2 ; cout << posOfCarryBit ( a , b ) ; return 0 ; }
Check whether the two numbers differ at one bit position only | C ++ implementation to check whether the two numbers differ at one bit position only ; function to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; function to check whether the two numbers differ at one bit position only ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerOfTwo ( unsigned int x ) { return x && ( ! ( x & ( x - 1 ) ) ) ; } bool differAtOneBitPos ( unsigned int a , unsigned int b ) { return isPowerOfTwo ( a ^ b ) ; } int main ( ) { unsigned int a = 13 , b = 9 ; if ( differAtOneBitPos ( a , b ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Multiplication with a power of 2 | Simple C / C ++ program to compute x * ( 2 ^ n ) ; Returns 2 raised to power n ; Driven program
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int ll ; ll power2 ( ll n ) { if ( n == 0 ) return 1 ; if ( n == 1 ) return 2 ; return power2 ( n / 2 ) * power2 ( n / 2 ) ; } ll multiply ( ll x , ll n ) { return x * power2 ( n ) ; } int main ( ) { ll x = 70 , n = 2 ; cout << multiply ( x , n ) ; return 0 ; }
Multiplication with a power of 2 | Efficient C / C ++ program to compute x * ( 2 ^ n ) ; Driven program to check above function
#include <stdio.h> NEW_LINE typedef long long int ll ; ll multiply ( ll x , ll n ) { return x << n ; } int main ( ) { ll x = 70 , n = 2 ; printf ( " % lld " , multiply ( x , n ) ) ; return 0 ; }
Check if n is divisible by power of 2 without using arithmetic operators | C ++ implementation to check whether n is divisible by pow ( 2 , m ) ; function to check whether n is divisible by pow ( 2 , m ) ; if expression results to 0 , then n is divisible by pow ( 2 , m ) ; n is not divisible ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivBy2PowerM ( unsigned int n , unsigned int m ) { if ( ( n & ( ( 1 << m ) - 1 ) ) == 0 ) return true ; return false ; } int main ( ) { unsigned int n = 8 , m = 2 ; if ( isDivBy2PowerM ( n , m ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Game of Nim with removal of one stone allowed | C ++ program for Game of Nim with removal of one stone allowed . ; Return true if player A wins , return false if player B wins . ; Checking the last bit of N . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findWinner ( int N ) { return N & 1 ; } int main ( ) { int N = 15 ; findWinner ( N ) ? ( cout << " Player ▁ A " ; ) : ( cout << " Player ▁ B " ; ) ; return 0 ; }
Toggle all odd bits of a number | Toggle all odd bit of a number ; Returns a number which has all odd bits of n toggled . ; Generate number form of 101010. . . . . till of same order as n ; if bit is odd , then generate number and or with res ; return toggled number ; Driver code
#include <iostream> NEW_LINE using namespace std ; int evenbittogglenumber ( int n ) { int res = 0 , count = 0 ; for ( int temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 0 ) res |= ( 1 << count ) ; count ++ ; } return n ^ res ; } int main ( ) { int n = 11 ; cout << evenbittogglenumber ( n ) ; return 0 ; }
Quotient and remainder dividing by 2 ^ k ( a power of 2 ) | CPP to find remainder and quotient ; function to print remainder and quotient ; print Remainder by n AND ( m - 1 ) ; print quotient by right shifting n by ( log2 ( m ) ) times ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void divide ( int n , int m ) { cout << " Remainder ▁ = ▁ " << ( ( n ) & ( m - 1 ) ) ; cout << " Quotient = " } int main ( ) { int n = 43 , m = 8 ; divide ( n , m ) ; return 0 ; }
Maximum AND value of a pair in an array | CPP Program to find maximum XOR value of a pair ; Function for finding maximum and value pair ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxAND ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) res = max ( res , arr [ i ] & arr [ j ] ) ; return res ; } int main ( ) { int arr [ ] = { 4 , 8 , 6 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Maximum ▁ AND ▁ Value ▁ = ▁ " << maxAND ( arr , n ) ; return 0 ; }
Check if a number is positive , negative or zero using bit operators | CPP program to find if a number is positive , negative or zero using bit wise operators . ; function to return 1 if it is zero returns 0 if it is negative returns 2 if it is positive ; string array to store all kinds of number ; function call to check the sign of number ; driver program to test the above function
#include <iostream> NEW_LINE using namespace std ; int index ( int i ) { return 1 + ( i >> 31 ) - ( - i >> 31 ) ; } void check ( int n ) { string s [ ] = { " negative " , " zero " , " positive " } ; int val = index ( n ) ; cout << n << " ▁ is ▁ " << s [ val ] << endl ; } int main ( ) { check ( 30 ) ; check ( -20 ) ; check ( 0 ) ; return 0 ; }
Find two numbers from their sum and XOR | CPP program to find two numbers with given Sum and XOR such that value of first number is minimum . ; Function that takes in the sum and XOR of two numbers and generates the two numbers such that the value of X is minimized ; Traverse through all bits ; Let us leave bits as 0. ; We leave i - th bit of b as 0. ; else ( Xi == 1 && Ai == 1 ) ; Driver function
#include <iostream> NEW_LINE using namespace std ; void compute ( unsigned long int S , unsigned long int X ) { unsigned long int A = ( S - X ) / 2 ; int a = 0 , b = 0 ; for ( int i = 0 ; i < 8 * sizeof ( S ) ; i ++ ) { unsigned long int Xi = ( X & ( 1 << i ) ) ; unsigned long int Ai = ( A & ( 1 << i ) ) ; if ( Xi == 0 && Ai == 0 ) { } else if ( Xi == 0 && Ai > 0 ) { a = ( ( 1 << i ) a ) ; b = ( ( 1 << i ) b ) ; } else if ( Xi > 0 && Ai == 0 ) { a = ( ( 1 << i ) a ) ; } { cout << " Not ▁ Possible " ; return ; } } cout << " a ▁ = ▁ " << a << endl << " b ▁ = ▁ " << b ; } int main ( ) { unsigned long int S = 17 , X = 13 ; compute ( S , X ) ; return 0 ; }
Divisibility by 64 with removal of bits allowed | CPP program to find if given binary string can become divisible by 64 after removing some bits . ; function to check if it is possible to make it a multiple of 64. ; counter to count 0 's ; length of the string ; loop which traverses right to left and calculates the number of zeros before 1. ; driver code
#include <iostream> NEW_LINE using namespace std ; bool checking ( string s ) { int c = 0 ; int n = s . length ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == '0' ) c ++ ; if ( c >= 6 and s [ i ] == '1' ) return true ; } return false ; } int main ( ) { string s = "100010001" ; if ( checking ( s ) ) cout << " Possible " ; else cout << " Not ▁ possible " ; return 0 ; }
Modify a bit at a given position | CPP program to modify a bit at position p in n to b . ; Returns modified n . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int modifyBit ( int n , int p , int b ) { int mask = 1 << p ; return ( ( n & ~ mask ) | ( b << p ) ) ; } int main ( ) { cout << modifyBit ( 6 , 2 , 0 ) << endl ; cout << modifyBit ( 6 , 5 , 1 ) << endl ; return 0 ; }
Count set bits in a range | C ++ implementation to count set bits in the given range ; Function to get no of set bits in the binary representation of ' n ' ; function to count set bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; returns number of set bits in the range ' l ' to ' r ' in ' n ' ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int countSetBits ( int n ) { unsigned int count = 0 ; while ( n ) { n &= ( n - 1 ) ; count ++ ; } return count ; } unsigned int countSetBitsInGivenRange ( unsigned int n , unsigned int l , unsigned int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return countSetBits ( n & num ) ; } int main ( ) { unsigned int n = 42 ; unsigned int l = 2 , r = 5 ; cout << countSetBitsInGivenRange ( n , l , r ) ; return 0 ; }
Check if one of the numbers is one 's complement of the other | C ++ implementation to check if one of the two numbers is one 's complement of the other ; function to check if all the bits are set or not in the binary representation of ' n ' ; all bits are not set ; if true , then all bits are set ; else all bits are not set ; function to check if one of the two numbers is one 's complement of the other ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool areAllBitsSet ( unsigned int n ) { if ( n == 0 ) return false ; if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } bool isOnesComplementOfOther ( unsigned int a , unsigned int b ) { return areAllBitsSet ( a ^ b ) ; } int main ( ) { unsigned int a = 10 , b = 5 ; if ( isOnesComplementOfOther ( a , b ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Unique element in an array where all elements occur k times except one | CPP program to find unique element where every element appears k times except one ; Create a count array to store count of numbers that have a particular bit set . count [ i ] stores count of array elements with i - th bit set . ; AND ( bitwise ) each element of the array with each set digit ( one at a time ) to get the count of set bits at each position ; Now consider all bits whose count is not multiple of k to form the required number . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findUnique ( unsigned int a [ ] , int n , int k ) { int INT_SIZE = 8 * sizeof ( unsigned int ) ; int count [ INT_SIZE ] ; memset ( count , 0 , sizeof ( count ) ) ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( ( a [ j ] & ( 1 << i ) ) != 0 ) count [ i ] += 1 ; unsigned res = 0 ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) res += ( count [ i ] % k ) * ( 1 << i ) ; return res ; } int main ( ) { unsigned int a [ ] = { 6 , 2 , 5 , 2 , 2 , 6 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int k = 3 ; cout << findUnique ( a , n , k ) ; return 0 ; }
Check whether the number has only first and last bits set | C ++ to check whether the number has only first and last bits set ; function to check whether ' n ' is a power of 2 or not ; function to check whether the number has only first and last bits set ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool powerOfTwo ( unsigned int n ) { return ( ! ( n & n - 1 ) ) ; } bool onlyFirstAndLastAreSet ( unsigned int n ) { if ( n == 1 ) return true ; if ( n == 2 ) return false ; return powerOfTwo ( n - 1 ) ; } int main ( ) { unsigned int n = 9 ; if ( onlyFirstAndLastAreSet ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if a number has bits in alternate pattern | Set | C ++ implementation to check if a number has bits in alternate pattern ; function to check if all the bits are set or not in the binary representation of ' n ' ; if true , then all bits are set ; else all bits are not set ; function to check if a number has bits in alternate pattern ; to check if all bits are set in ' num ' ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool allBitsAreSet ( unsigned int n ) { if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } bool bitsAreInAltOrder ( unsigned int n ) { unsigned int num = n ^ ( n >> 1 ) ; return allBitsAreSet ( num ) ; } int main ( ) { unsigned int n = 10 ; if ( bitsAreInAltOrder ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimum flips required to maximize a number with k set bits | CPP for finding min flip for maximizing given n ; function for finding set bit ; return count of set bit ; function for finding min flip ; number of bits in n ; Find the largest number of same size with k set bits ; Count bit differences to find required flipping . ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int setBit ( int xorValue ) { int count = 0 ; while ( xorValue ) { if ( xorValue % 2 ) count ++ ; xorValue /= 2 ; } return count ; } int minFlip ( int n , int k ) { int size = log2 ( n ) + 1 ; int max = pow ( 2 , k ) - 1 ; max = max << ( size - k ) ; int xorValue = ( n ^ max ) ; return ( setBit ( xorValue ) ) ; } int main ( ) { int n = 27 , k = 3 ; cout << " Min ▁ Flips ▁ = ▁ " << minFlip ( n , k ) ; return 0 ; }
Set all the bits in given range of a number | C ++ implementation to Set bits in the given range ; function to toggle bits in the given range ; calculating a number ' range ' having set bits in the range from l to r and all other bits as 0 ( or unset ) . ; Driver code
#include <iostream> NEW_LINE using namespace std ; int setallbitgivenrange ( int n , int l , int r ) { int range = ( ( ( 1 << ( l - 1 ) ) - 1 ) ^ ( ( 1 << ( r ) ) - 1 ) ) ; return ( n range ) ; } int main ( ) { int n = 17 , l = 2 , r = 3 ; cout << setallbitgivenrange ( n , l , r ) ; return 0 ; }
Count total bits in a number | C ++ program to find total bit in given number ; log function in base 2 take only integer part ; Driven program
#include <iostream> NEW_LINE #include <cmath> NEW_LINE unsigned countBits ( unsigned int number ) { return ( int ) log2 ( number ) + 1 ; } int main ( ) { unsigned int num = 65 ; std :: cout << countBits ( num ) << ' ' ; return 0 ; }
Check whether all the bits are unset in the given range or not | C ++ implementation to check whether all the bits are unset in the given range or not ; function to check whether all the bits are unset in the given range or not ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; new number which will only have one or more set bits in the range l to r and nowhere else ; if new num is 0 , then all bits are unset in the given range ; else all bits are not unset ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; string 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 ( new_num == 0 ) return " Yes " ; return " No " ; } int main ( ) { unsigned int n = 17 ; unsigned int l = 2 , r = 4 ; cout << allBitsSetInTheGivenRange ( n , l , r ) ; return 0 ; }
Toggle all bits after most significant bit | CPP program to toggle set bits starting from MSB ; Returns a number which has all set bits starting from MSB of n ; This makes sure two bits ( From MSB and including MSB ) are set ; This makes sure 4 bits ( From MSB and including MSB ) are set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int setAllBitsAfterMSB ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return n ; } void toggle ( int & n ) { n = n ^ setAllBitsAfterMSB ( n ) ; } int main ( ) { int n = 10 ; toggle ( n ) ; cout << n ; return 0 ; }
Check if a number has two adjacent set bits | CPP program to check if there are two adjacent set bits . ; Driver Code
#include <iostream> NEW_LINE using namespace std ; bool adjacentSet ( int n ) { return ( n & ( n >> 1 ) ) ; } int main ( ) { int n = 3 ; adjacentSet ( n ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Position of rightmost common bit in two numbers | C ++ implementation to find the position of rightmost same bit ; Function to find the position of rightmost set bit in ' n ' ; Function to find the position of rightmost same bit in the binary representations of ' m ' and ' n ' ; position of rightmost same bit ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getRightMostSetBit ( unsigned int n ) { return log2 ( n & - n ) + 1 ; } int posOfRightMostSameBit ( unsigned int m , unsigned int n ) { return getRightMostSetBit ( ~ ( m ^ n ) ) ; } int main ( ) { int m = 16 , n = 7 ; cout << " Position ▁ = ▁ " << posOfRightMostSameBit ( m , n ) ; return 0 ; }
Position of rightmost common bit in two numbers | C ++ implementation to find the position of rightmost same bit ; Function to find the position of rightmost same bit in the binary representations of ' m ' and ' n ' ; Initialize loop counter ; Check whether the value ' m ' is odd ; Check whether the value ' n ' is odd ; Below ' if ' checks for both values to be odd or even ; Right shift value of m ; Right shift value of n ; When no common set is found ; Driver code
#include <iostream> NEW_LINE using namespace std ; static int posOfRightMostSameBit ( int m , int n ) { int loopCounter = 1 ; while ( m > 0 n > 0 ) { bool a = m % 2 == 1 ; bool b = n % 2 == 1 ; if ( ! ( a ^ b ) ) { return loopCounter ; } m = m >> 1 ; n = n >> 1 ; loopCounter ++ ; } return -1 ; } int main ( ) { int m = 16 , n = 7 ; cout << " Position ▁ = ▁ " << posOfRightMostSameBit ( m , n ) ; }