codeparrot/xlcost-text-to-code huggingface dataset:
Toggle first and last bits of a number | C # 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
using System ; class GFG { static int takeLandFsetbits ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( ( n + 1 ) >> 1 ) + 1 ; } static int toggleFandLbits ( int n ) { if ( n == 1 ) return 0 ; return n ^ takeLandFsetbits ( n ) ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( toggleFandLbits ( n ) ) ; } }
Odious number | 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
using System ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n != 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static bool checkOdious ( int n ) { return ( countSetBits ( n ) % 2 == 1 ) ; } public static void Main ( ) { int num = 32 ; if ( checkOdious ( num ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
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
using System ; class GFG { static 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 ) ) ) ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( setleftmostunsetbit ( n ) ) ; } }
Maximum XOR using K numbers from 1 to n | C # 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
using System ; public class main { static int maxXorSum ( int n , int k ) { if ( k == 1 ) return n ; int res = 1 ; while ( res <= n ) res <<= 1 ; return res - 1 ; } public static void Main ( ) { int n = 4 , k = 3 ; Console . WriteLine ( maxXorSum ( n , k ) ) ; } }
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
using System ; class GFG { static int getPosOfRightmostSetBit ( int n ) { return ( int ) ( Math . Log ( n & - n ) / Math . Log ( 2 ) ) ; } static int toggleLastKBits ( int n , int k ) { int num = ( 1 << k ) - 1 ; return ( n ^ num ) ; } static int incrementByOne ( int n ) { int k = getPosOfRightmostSetBit ( ~ n ) ; n = ( ( 1 << k ) n ) ; if ( k != 0 ) n = toggleLastKBits ( n , k ) ; return n ; } public static void Main ( ) { int n = 15 ; Console . WriteLine ( incrementByOne ( n ) ) ; } }
XNOR of two numbers | C # program to find XNOR of two numbers ; Make sure a is larger ; swapping a and b ; ; 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 function
using System ; public class GfG { public static int xnor ( int a , int b ) { if ( a < b ) { int t = a ; a = b ; b = t ; } if ( a == 0 && b == 0 ) return 1 ; int a_rem = 0 ; int b_rem = 0 ; int count = 0 ; int xnornum = 0 ; while ( true ) { a_rem = a & 1 ; b_rem = b & 1 ; if ( a_rem == b_rem ) xnornum |= ( 1 << count ) ; count ++ ; a = a >> 1 ; b = b >> 1 ; if ( a < 1 ) break ; } return xnornum ; } public static void Main ( ) { int a = 10 , b = 50 ; Console . WriteLine ( xnor ( a , b ) ) ; } }
XNOR of two numbers | C # 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 program
using System ; class GFG { static 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 ; } static int xnor ( int num1 , int num2 ) { if ( num1 < num2 ) { int temp = num1 ; num1 = num2 ; num2 = temp ; } num1 = togglebit ( num1 ) ; return num1 ^ num2 ; } public static void Main ( ) { int a = 10 , b = 20 ; Console . WriteLine ( xnor ( a , b ) ) ; } }
Maximum OR sum of sub | C # program to find maximum OR sum ; function to find maximum OR sum ; OR sum of all the elements in both arrays ; Driver code
using System ; class GFG { static 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 ] ; } Console . WriteLine ( sum1 + sum2 ) ; } public static void Main ( ) { int [ ] A = { 1 , 2 , 4 , 3 , 2 } ; int [ ] B = { 2 , 3 , 3 , 12 , 1 } ; int n = A . Length ; MaximumSum ( A , B , n ) ; } }
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 code
using System ; class GFG { static int posOfRightmostSetBit ( int n ) { return ( int ) ( Math . Log ( n & - n ) / Math . Log ( 2 ) ) + 1 ; } static int posOfCarryBit ( int a , int b ) { return posOfRightmostSetBit ( a & b ) ; } public static void Main ( ) { int a = 10 , b = 2 ; Console . Write ( posOfCarryBit ( a , b ) ) ; } }
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 code
using System ; class GFG { static bool isPowerOfTwo ( int x ) { return x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ; } static bool differAtOneBitPos ( int a , int b ) { return isPowerOfTwo ( a ^ b ) ; } public static void Main ( ) { int a = 13 , b = 9 ; if ( differAtOneBitPos ( a , b ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Multiplication with a power of 2 | Simple C # program to compute x * ( 2 ^ n ) ; Returns 2 raised to power n ; Driver program
using System ; class GFG { static long power2 ( long n ) { if ( n == 0 ) return 1 ; if ( n == 1 ) return 2 ; return power2 ( n / 2 ) * power2 ( n / 2 ) ; } static long multiply ( long x , long n ) { return x * power2 ( n ) ; } public static void Main ( ) { long x = 70 , n = 2 ; Console . WriteLine ( multiply ( x , n ) ) ; } }
Multiplication with a power of 2 | C # Code for Multiplication with a power of 2 ; Driver program to test above function
using System ; class GFG { static int multiply ( int x , int n ) { return x << n ; } public static void Main ( ) { int x = 70 , n = 2 ; Console . WriteLine ( multiply ( x , n ) ) ; } }
Check if n is divisible by power of 2 without using arithmetic operators | C # Code for Check if n is divisible by power of 2 without using arithmetic operators ; 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 function
using System ; class GFG { static bool isDivBy2PowerM ( int n , int m ) { if ( ( n & ( ( 1 << m ) - 1 ) ) == 0 ) return true ; return false ; } public static void Main ( ) { int n = 8 , m = 2 ; if ( isDivBy2PowerM ( n , m ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Game of Nim with removal of one stone allowed | C # Code 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 . ; Driver program to test above function
using System ; class GFG { static int findWinner ( int N ) { return N & 1 ; } public static void Main ( ) { int N = 15 ; if ( findWinner ( N ) == 1 ) Console . Write ( " Player ▁ A " ) ; else Console . Write ( " Player ▁ B " ) ; } }
Toggle all odd bits of a number | C # code for 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
using System ; class GFG { static 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 ; } public static void Main ( ) { int n = 11 ; Console . WriteLine ( evenbittogglenumber ( n ) ) ; } }
Quotient and remainder dividing by 2 ^ k ( a power of 2 ) | C # 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
using System ; public class GFG { static void divide ( int n , int m ) { Console . WriteLine ( " Remainder ▁ = ▁ " + ( ( n ) & ( m - 1 ) ) ) ; Console . WriteLine ( " Quotient ▁ = ▁ " + ( n >> ( int ) ( Math . Log ( m ) ) ) ) ; } static public void Main ( ) { int n = 43 , m = 8 ; divide ( n , m ) ; } }
Maximum AND value of a pair in an array | C # Program to find maximum XOR value of a pair ; Function for finding maximum and value pair ; Driver code
using System ; public class GfG { static 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 = res > ( arr [ i ] & arr [ j ] ) ? res : ( arr [ i ] & arr [ j ] ) ; return res ; } public static void Main ( ) { int [ ] arr = { 4 , 8 , 6 , 2 } ; int n = arr . Length ; Console . WriteLine ( " Maximum ▁ AND ▁ Value ▁ = ▁ " + maxAND ( arr , n ) ) ; } }
Check if a number is positive , negative or zero using bit operators | C # 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 code
using System ; class GFG { static int index ( int i ) { return 1 + ( i >> 31 ) - ( - i >> 31 ) ; } static void check ( int n ) { String [ ] s = { " negative " , " zero " , " positive " } ; int val = index ( n ) ; Console . WriteLine ( n + " ▁ is ▁ " + s [ val ] ) ; } public static void Main ( ) { check ( 30 ) ; check ( - 20 ) ; check ( 0 ) ; } }
Find two numbers from their sum and XOR | C # 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
using System ; public class GFG { static void compute ( long S , long X ) { long A = ( S - X ) / 2 ; int a = 0 , b = 0 ; for ( int i = 0 ; i < 8 * sizeof ( long ) ; i ++ ) { long Xi = ( X & ( 1 << i ) ) ; long 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 ) ; } { Console . WriteLine ( " Not ▁ Possible " ) ; return ; } } Console . WriteLine ( " a ▁ = ▁ " + a + " b = " } public static void ( ) { long S = 17 , X = 13 ; compute ( S , X ) ; } }
Divisibility by 64 with removal of bits allowed | C # 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
using System ; class GFG { static 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 && s [ i ] == '1' ) return true ; } return false ; } public static void Main ( ) { String s = "100010001" ; if ( checking ( s ) ) Console . WriteLine ( " Possible " ) ; else Console . WriteLine ( " Not ▁ possible " ) ; } }
Modify a bit at a given position | C # program to modify a bit at position p in n to b . ; Returns modified n . ; Driver Code
using System ; class GFG { public static int modifyBit ( int n , int p , int b ) { int mask = 1 << p ; return ( n & ~ mask ) | ( ( b << p ) & mask ) ; } static public void Main ( ) { Console . WriteLine ( modifyBit ( 6 , 2 , 0 ) ) ; Console . WriteLine ( modifyBit ( 6 , 5 , 1 ) ) ; } }
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 code
using System ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int countSetBitsInGivenRange ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return countSetBits ( n & num ) ; } public static void Main ( ) { int n = 42 ; int l = 2 , r = 5 ; Console . WriteLine ( countSetBitsInGivenRange ( n , l , r ) ) ; } }
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 function
using System ; class GFG { public static bool areAllBitsSet ( long n ) { if ( n == 0 ) return false ; if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } public static bool isOnesComplementOfOther ( long a , long b ) { return areAllBitsSet ( a ^ b ) ; } public static void Main ( ) { long a = 10 , b = 5 ; if ( isOnesComplementOfOther ( a , b ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Unique element in an array where all elements occur k times except one | C # 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
using System ; class GFG { static int findUnique ( int [ ] a , int n , int k ) { byte sizeof_int = 4 ; int INT_SIZE = 8 * sizeof_int ; int [ ] count = new int [ INT_SIZE ] ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( ( a [ j ] & ( 1 << i ) ) != 0 ) count [ i ] += 1 ; int res = 0 ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) res += ( count [ i ] % k ) * ( 1 << i ) ; return res ; } public static void Main ( String [ ] args ) { int [ ] a = { 6 , 2 , 5 , 2 , 2 , 6 , 6 } ; int n = a . Length ; int k = 3 ; Console . WriteLine ( findUnique ( a , n , k ) ) ; } }
Check whether the number has only first and last bits set | C # program 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
using System ; class GFG { static bool powerOfTwo ( uint n ) { return ( ( n & n - 1 ) == 0 ) ; } static bool onlyFirstAndLastAreSet ( uint n ) { if ( n == 1 ) return true ; return powerOfTwo ( n - 1 ) ; } public static void Main ( ) { uint n = ( uint ) 9 ; if ( onlyFirstAndLastAreSet ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
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 Code
using System ; class GFG { static bool allBitsAreSet ( int n ) { if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } static bool bitsAreInAltOrder ( int n ) { int num = n ^ ( n >> 1 ) ; return allBitsAreSet ( num ) ; } public static void Main ( ) { int n = 10 ; if ( bitsAreInAltOrder ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Minimum flips required to maximize a number with k set bits | C # Code to find Minimum flips required to maximize a number with k set bits ; 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 Code
using System ; class GFG { static int setBit ( int xorValue ) { int count = 0 ; while ( xorValue >= 1 ) { if ( xorValue % 2 == 1 ) count ++ ; xorValue /= 2 ; } return count ; } static int minFlip ( int n , int k ) { int size = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) + 1 ; int max = ( int ) Math . Pow ( 2 , k ) - 1 ; max = max << ( size - k ) ; int xorValue = ( n ^ max ) ; return ( setBit ( xorValue ) ) ; } public static void Main ( ) { int n = 27 , k = 3 ; Console . Write ( " Min ▁ Flips ▁ = ▁ " + minFlip ( n , k ) ) ; } }
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
using System ; class GFG { static int setallbitgivenrange ( int n , int l , int r ) { int range = ( ( ( 1 << ( l - 1 ) ) - 1 ) ^ ( ( 1 << ( r ) ) - 1 ) ) ; return ( n range ) ; } static void Main ( ) { int n = 17 , l = 2 , r = 3 ; Console . Write ( setallbitgivenrange ( n , l , r ) ) ; } }
Count total bits in a number | C # program to find total bit in given number ; log function in base 2 take only integer part ; Driver code
using System ; class GFG { static uint countBits ( uint number ) { return ( uint ) Math . Log ( number , 2.0 ) + 1 ; } public static void Main ( ) { uint num = 65 ; Console . WriteLine ( countBits ( num ) ) ; } }
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 Code
using System ; public class GFG { static String allBitsSetInTheGivenRange ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; int new_num = n & num ; if ( new_num == 0 ) return " Yes " ; return " No " ; } static public void Main ( ) { int n = 17 ; int l = 2 ; int r = 4 ; Console . WriteLine ( allBitsSetInTheGivenRange ( n , l , r ) ) ; } }
Toggle all bits after most significant bit | C # 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
using System ; class GFG { static int setAllBitsAfterMSB ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return n ; } static int toggle ( int n ) { n = n ^ setAllBitsAfterMSB ( n ) ; return n ; } public static void Main ( ) { int n = 10 ; n = toggle ( n ) ; Console . WriteLine ( n ) ; } }
Check if a number has two adjacent set bits | C # program to check if there are two adjacent set bits . ; Driver code
using System ; class GFG { static bool adjacentSet ( int n ) { int x = ( n & ( n >> 1 ) ) ; if ( x > 0 ) return true ; else return false ; } public static void Main ( ) { int n = 3 ; if ( adjacentSet ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
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 code
using System ; class GFG { static int getRightMostSetBit ( int n ) { return ( int ) ( ( Math . Log ( n & - n ) ) / ( Math . Log ( 2 ) ) ) + 1 ; } static int posOfRightMostSameBit ( int m , int n ) { return getRightMostSetBit ( ~ ( m ^ n ) ) ; } public static void Main ( ) { int m = 16 , n = 7 ; Console . Write ( " Position ▁ = ▁ " + posOfRightMostSameBit ( m , n ) ) ; } }
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
using System ; class GFG { static int posOfRightMostSameBit ( int m , int n ) { int loopCounter = 1 ; while ( m > 0 n > 0 ) { Boolean a = m % 2 == 1 ; Boolean b = n % 2 == 1 ; if ( ! ( a ^ b ) ) { return loopCounter ; } m = m >> 1 ; n = n >> 1 ; loopCounter ++ ; } return - 1 ; } public static void Main ( String [ ] args ) { int m = 16 , n = 7 ; Console . Write ( " Position ▁ = ▁ " + posOfRightMostSameBit ( m , n ) ) ; } }