codeparrot/xlcost-text-to-code huggingface dataset:
Bitwise operations on Subarrays of size K | C # program for minimum values of each bitwise AND operation on elements of subarray of size K ; Function to convert bit array to decimal number ; Function to find minimum values of each bitwise AND operation on element of subarray of size K ; Maintain an integer array bit [ ] of size 32 all initialized to 0 ; Create a sliding window of size k ; Function call ; Perform operation to removed element ; Perform operation to add element ; Taking minimum value ; Return the result ; Driver Code ; Given array [ ] arr ; Given subarray size K ; Function call
using System ; class GFG { static int build_num ( int [ ] bit , int k ) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] == k ) ans += ( 1 << i ) ; return ans ; } static int minimumAND ( int [ ] arr , int n , int k ) { int [ ] bit = new int [ 32 ] ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } } int min_and = build_num ( bit , k ) ; for ( int i = k ; i < n ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i - k ] & ( 1 << j ) ) > 0 ) bit [ j ] -- ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } min_and = Math . Min ( build_num ( bit , k ) , min_and ) ; } return min_and ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 5 , 3 , 6 , 11 , 13 } ; int k = 3 ; int n = arr . Length ; Console . Write ( minimumAND ( arr , n , k ) ) ; } }
Equal Sum and XOR of three Numbers | C # implementation of the approach ; Function to calculate power of 3 ; Function to return the count of the unset bit ( zeros ) ; Check the bit is 0 or not ; Right shifting ( dividing by 2 ) ; Driver Code
using System ; class GFG { static long calculate ( int bit_cnt ) { long res = 1 ; while ( bit_cnt -- > 0 ) { res = res * 3 ; } return res ; } static int unset_bit_count ( long n ) { int count = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) count ++ ; n = n >> 1 ; } return count ; } public static void Main ( String [ ] args ) { long n ; n = 2 ; int count = unset_bit_count ( n ) ; long ans = calculate ( count ) ; Console . WriteLine ( ans ) ; } }
Count pairs of elements such that number of set bits in their AND is B [ i ] | C # Implementation of the above approach ; Function to return the count of pairs which satisfy the given condition ; Check if the count of set bits in the AND value is B [ j ] ; Function to get no of set bits in binary representation of positive integer n ; Driver code
using System ; class GFG { static int solve ( int [ ] A , int [ ] B , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( countSetBits ( A [ i ] & A [ j ] ) == B [ j ] ) { cnt ++ ; } } } return cnt ; } static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 3 , 1 , 4 , 5 } ; int [ ] B = { 2 , 2 , 1 , 4 , 2 } ; int size = A . Length ; Console . WriteLine ( solve ( A , B , size ) ) ; } }
Total pairs in an array such that the bitwise AND , bitwise OR and bitwise XOR of LSB is 1 | C # implementation of the approach ; Function to find the count of required pairs ; To store the count of elements which give remainder 0 i . e . even values ; To store the count of elements which give remainder 1 i . e . odd values ; Driver code
using System ; class GFG { static void CalculatePairs ( int [ ] a , int n ) { int cnt_zero = 0 ; int cnt_one = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 0 ) cnt_zero += 1 ; else cnt_one += 1 ; } int total_XOR_pairs = cnt_zero * cnt_one ; int total_AND_pairs = ( cnt_one ) * ( cnt_one - 1 ) / 2 ; int total_OR_pairs = cnt_zero * cnt_one + ( cnt_one ) * ( cnt_one - 1 ) / 2 ; Console . WriteLine ( " cntXOR ▁ = ▁ " + total_XOR_pairs ) ; Console . WriteLine ( " cntAND ▁ = ▁ " + total_AND_pairs ) ; Console . WriteLine ( " cntOR ▁ = ▁ " + total_OR_pairs ) ; } public static void Main ( ) { int [ ] a = { 1 , 3 , 4 , 2 } ; int n = a . Length ; CalculatePairs ( a , n ) ; } }
Assign other value to a variable from two possible values | C # program to change value of x according to its current value . ; Function to alternate the values ; Main function
using System ; class gfg { public void alternate ( ref int a , ref int b , ref int x ) { x = a + b - x ; } } class geek { public static int Main ( ) { gfg g = new gfg ( ) ; int a = - 10 ; int b = 15 ; int x = a ; Console . WriteLine ( " x ▁ is ▁ : ▁ { 0 } " , x ) ; g . alternate ( ref a , ref b , ref x ) ; Console . WriteLine ( " After ▁ change ▁ " ) ; Console . WriteLine ( " x ▁ is ▁ : ▁ { 0 } " , x ) ; return 0 ; } }
Highest power of two that divides a given number | C # program to find highest power of 2 that divides n . ; Driver code
using System ; class GFG { static int highestPowerOf2 ( int n ) { return ( n & ( ~ ( n - 1 ) ) ) ; } public static void Main ( ) { int n = 48 ; Console . Write ( highestPowerOf2 ( n ) ) ; } }
Check whether bitwise AND of a number with any subset of an array is zero or not | C # program to check whether bitwise AND of a number with any subset of an array is zero or not ; Function to check whether bitwise AND of a number with any subset of an array is zero or not ; variable to store the AND of all the elements ; find the AND of all the elements of the array ; if the AND of all the array elements and N is equal to zero ; Driver Code
using System ; class GFG { static void isSubsetAndZero ( int [ ] array , int length , int N ) { int arrAnd = array [ 0 ] ; for ( int i = 1 ; i < length ; i ++ ) { arrAnd = arrAnd & array [ i ] ; } if ( ( arrAnd & N ) == 0 ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } public static void Main ( ) { int [ ] array = { 1 , 2 , 4 } ; int length = array . Length ; int N = 3 ; isSubsetAndZero ( array , length , N ) ; } }
Finding the Parity of a number Efficiently | Program to find the parity of a given number ; Function to find the parity ; Rightmost bit of y holds the parity value if ( y & 1 ) is 1 then parity is odd else even ; Driver code
using System ; class GFG { static bool findParity ( int x ) { int y = x ^ ( x >> 1 ) ; y = y ^ ( y >> 2 ) ; y = y ^ ( y >> 4 ) ; y = y ^ ( y >> 8 ) ; y = y ^ ( y >> 16 ) ; if ( ( y & 1 ) > 0 ) return true ; return false ; } public static void Main ( ) { if ( ( findParity ( 9 ) == false ) ) Console . WriteLine ( " Even ▁ Parity " ) ; else Console . WriteLine ( " Odd ▁ Parity " ) ; if ( findParity ( 13 ) == false ) Console . WriteLine ( " Even ▁ Parity " ) ; else Console . WriteLine ( " Odd ▁ Parity " ) ; } }
Check if bits in range L to R of two numbers are complement of each other or not | C # implementation to check whether all the bits in the given range of two numbers are complement of each other ; function to check whether all the bits are set 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 both are equal , then all bits are set in the given range ; else all bits are not set ; function to check whether all the bits in the given range of two numbers are complement of each other ; Driver Code
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 " ) ; } }
Sum of the series 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + ... . . + 2 ^ n | C # program to find sum ; function to calculate sum of series ; initialize sum as 0 ; loop to calculate sum of series ; calculate 2 ^ i and add it to sum ; Driver code
using System ; class GFG { static int calculateSum ( int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( 1 << i ) ; } return sum ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( " Sum ▁ of ▁ the ▁ series ▁ : ▁ " + calculateSum ( n ) ) ; } }
Print all the combinations of N elements by changing sign such that their sum is divisible by M | Print all the combinations of N elements by changing sign such that their sum is divisible by M ; Function to print all the combinations ; Iterate for all combinations ; Initially 100 in binary if n is 3 as 1 << ( 3 - 1 ) = 100 in binary ; Iterate in the array and assign signs to the array elements ; If the j - th bit from left is set take ' + ' sign ; Right shift to check if jth bit is set or not ; re - initialize ; Iterate in the array elements ; If the jth from left is set ; right shift ; Driver code
using System ; class GFG { static void printCombinations ( int [ ] a , int n , int m ) { for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int sum = 0 ; int num = 1 << ( n - 1 ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & num ) > 0 ) sum += a [ j ] ; else sum += ( - 1 * a [ j ] ) ; num = num >> 1 ; } if ( sum % m == 0 ) { num = 1 << ( n - 1 ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & num ) > 0 ) Console . Write ( " + " + a [ j ] + " ▁ " ) ; else Console . Write ( " - " + a [ j ] + " ▁ " ) ; num = num >> 1 ; } Console . Write ( " STRNEWLINE " ) ; } } } public static void Main ( ) { int [ ] a = { 3 , 5 , 6 , 8 } ; int n = a . Length ; int m = 5 ; printCombinations ( a , n , m ) ; } }
Same Number Of Set Bits As N | C # program to find numbers less than N that have same Number Of Set Bits As N ; returns number of set bits in a number ; function ; __builtin_popcount function that count set bits in n ; Iterate from n - 1 to 1 ; check if the number of set bits equals to temp increment count ; Driver Code
using System ; class GFG { static int __builtin_popcount ( int n ) { int d , t = 0 ; while ( n > 0 ) { d = n % 2 ; n = n / 2 ; if ( d == 1 ) t ++ ; } return t ; } static int smallerNumsWithSameSetBits ( int n ) { int temp = __builtin_popcount ( n ) ; int count = 0 ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( temp == __builtin_popcount ( i ) ) count ++ ; } return count ; } static public void Main ( String [ ] args ) { int n = 4 ; Console . WriteLine ( smallerNumsWithSameSetBits ( n ) ) ; } }
Multiply any Number with 4 using Bitwise Operator | C # program to multiply a number with 4 using Bitwise Operator ; function the return multiply a number with 4 using bitwise operator ; returning a number with multiply with 4 using 2 bit shifting right ; Driver Code
using System ; class GFG { static int multiplyWith4 ( int n ) { return ( n << 2 ) ; } public static void Main ( String [ ] args ) { int n = 4 ; Console . Write ( multiplyWith4 ( n ) ) ; } }
Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs | C # program to print the Leftover element after performing alternate Bitwise OR and Bitwise XOR operations to the pairs . ; array to store the tree ; array to store the level of every parent ; function to construct the tree ; level of child is always 0 ; recursive call ; increase the level of every parent , which is level of child + 1 ; if the parent is at odd level , then do a bitwise OR ; if the parent is at even level , then do a bitwise XOR ; function that updates the tree ; if it is a leaf and the leaf which is to be updated ; out of range ; not a leaf then recurse ; recursive call ; check if the parent is at odd or even level and perform OR or XOR according to that ; function that assigns value to a [ index ] and calls update function to update the tree ; Driver Code ; builds the tree ; 1 st query ; 2 nd query
using System ; public class GFG { static int N = 1000 ; static int [ ] tree = new int [ N ] ; static int [ ] level = new int [ N ] ; static void constructTree ( int low , int high , int pos , int [ ] a ) { if ( low == high ) { level [ pos ] = 0 ; tree [ pos ] = a [ high ] ; return ; } int mid = ( low + high ) / 2 ; constructTree ( low , mid , 2 * pos + 1 , a ) ; constructTree ( mid + 1 , high , 2 * pos + 2 , a ) ; level [ pos ] = level [ 2 * pos + 1 ] + 1 ; if ( ( level [ pos ] & 1 ) > 0 ) tree [ pos ] = tree [ 2 * pos + 1 ] | tree [ 2 * pos + 2 ] ; else tree [ pos ] = tree [ 2 * pos + 1 ] ^ tree [ 2 * pos + 2 ] ; } static void update ( int low , int high , int pos , int index , int [ ] a ) { if ( low == high && low == index ) { tree [ pos ] = a [ low ] ; return ; } if ( index < low index > high ) return ; if ( low != high ) { int mid = ( low + high ) / 2 ; update ( low , mid , 2 * pos + 1 , index , a ) ; update ( mid + 1 , high , 2 * pos + 2 , index , a ) ; if ( ( level [ pos ] & 1 ) > 0 ) tree [ pos ] = tree [ 2 * pos + 1 ] | tree [ 2 * pos + 2 ] ; else tree [ pos ] = tree [ 2 * pos + 1 ] ^ tree [ 2 * pos + 2 ] ; } } static void updateValue ( int index , int value , int [ ] a , int n ) { a [ index ] = value ; update ( 0 , n - 1 , 0 , index , a ) ; } static public void Main ( ) { int [ ] a = { 1 , 4 , 5 , 6 } ; int n = a . Length ; ; constructTree ( 0 , n - 1 , 0 , a ) ; int index = 0 ; int value = 2 ; updateValue ( index , value , a , n ) ; Console . WriteLine ( tree [ 0 ] ) ; index = 3 ; value = 5 ; updateValue ( index , value , a , n ) ; Console . WriteLine ( tree [ 0 ] ) ; } }
Set all even bits of a number | Simple C # program to set all even bits of a number ; Sets even bits of n and returns modified number . ; Generate 101010. . .10 number and store in res . ; if bit is even then generate number and or with res ; return OR number ; Driver code
using System ; class GFG { static int evenbitsetnumber ( int n ) { int count = 0 , res = 0 ; for ( int temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 1 ) res |= ( 1 << count ) ; count ++ ; } return ( n res ) ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( evenbitsetnumber ( n ) ) ; } }
Set all even bits of a number | Efficient C # program to set all even bits of a number ; return msb set number ; set all bits ; return msb increment n by 1 and shift by 1 ; return even seted number ; get msb here ; generate even bits like 101010. . ; if bits is odd then shift by 1 ; return even set bits number ; set all even bits here ; take or with even set bits number ; Driver Code
using System ; class GFG { static int getmsb ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( n + 1 ) >> 1 ; } static int getevenbits ( int n ) { n = getmsb ( n ) ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; if ( ( n & 1 ) == 1 ) n = n >> 1 ; return n ; } static int setallevenbits ( int n ) { return n | getevenbits ( n ) ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( setallevenbits ( n ) ) ; } }
Set all odd bits of a number | C # code to Set all odd bits of a number ; set all odd bit ; res for store 010101. . number ; generate number form of 010101. . ... till temp size ; if bit is odd , then generate number and or with res ; Driver Code
using System ; class GFG { static int oddbitsetnumber ( int n ) { int count = 0 ; int res = 0 ; for ( int temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 0 ) res |= ( 1 << count ) ; count ++ ; } return ( n res ) ; } static public void Main ( ) { int n = 10 ; Console . WriteLine ( oddbitsetnumber ( n ) ) ; } }
Set all odd bits of a number | Efficient C # program to set all odd bits number ; return MSB set number ; set all bits including MSB . ; return MSB ; Returns a number of same size ( MSB at same position ) as n and all odd bits set . ; generate odd bits like 010101. . ; if bits is even then shift by 1 ; return odd set bits number ; set all odd bits here ; take OR with odd set bits number ; Driver Code
using System ; class GFG { static int getmsb ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( n + 1 ) >> 1 ; } static int getevenbits ( int n ) { n = getmsb ( n ) ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; if ( ( n & 1 ) == 0 ) n = n >> 1 ; return n ; } static int setalloddbits ( int n ) { return n | getevenbits ( n ) ; } static public void Main ( ) { int n = 10 ; Console . WriteLine ( setalloddbits ( n ) ) ; } }
Print numbers in the range 1 to n having bits in alternate pattern | C # implementation to print numbers in the range 1 to n having bits in alternate pattern ; function to print numbers in the range 1 to n having bits in alternate pattern ; first number having bits in alternate pattern ; display ; loop until n < curr_num ; generate next number having alternate bit pattern ; if true then break ; display ; generate next number having alternate bit pattern ; if true then break ; display ; Driver code
using System ; class GFG { public static void printNumHavingAltBitPatrn ( int n ) { int curr_num = 1 , i = 1 ; Console . Write ( curr_num + " ▁ " ) ; while ( i != 0 ) { curr_num <<= 1 ; if ( n < curr_num ) break ; Console . Write ( curr_num + " ▁ " ) ; curr_num = ( ( curr_num ) << 1 ) ^ 1 ; if ( n < curr_num ) break ; Console . Write ( curr_num + " ▁ " ) ; } } public static void Main ( ) { int n = 50 ; printNumHavingAltBitPatrn ( n ) ; } }
Smallest perfect power of 2 greater than n ( without using arithmetic operators ) | C # Code for Smallest perfect power of 2 greater than n ; Function to find smallest perfect power of 2 greater than n ; To store perfect power of 2 ; bitwise left shift by 1 ; bitwise right shift by 1 ; Required perfect power of 2 ; Driver program
using System ; class GFG { static int perfectPowerOf2 ( int n ) { int per_pow = 1 ; while ( n > 0 ) { per_pow = per_pow << 1 ; n = n >> 1 ; } return per_pow ; } public static void Main ( ) { int n = 128 ; Console . WriteLine ( " Perfect ▁ power ▁ of ▁ 2 ▁ greater ▁ than ▁ " + n + " : ▁ " + perfectPowerOf2 ( n ) ) ; } }
Find Unique pair in an array with pairs of numbers | C # program to find a unique pair in an array of pairs . ; XOR each element and get XOR of two unique elements ( ans ) ; Get a set bit of XOR ( We get the rightmost set bit ) ; Now divide elements in two sets by comparing rightmost set bit of XOR with bit at same position in each element . Initialize missing numbers ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; Driver code
using System ; class GFG { static void findUniquePair ( int [ ] arr , int n ) { int XOR = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) XOR = XOR ^ arr [ i ] ; int set_bit_no = XOR & ~ ( XOR - 1 ) ; int x = 0 , y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & set_bit_no ) > 0 ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } Console . WriteLine ( " The ▁ unique ▁ pair ▁ is ▁ ( " + x + " , ▁ " + y + " ) " ) ; } public static void Main ( ) { int [ ] a = { 6 , 1 , 3 , 5 , 1 , 3 , 7 , 6 } ; int n = a . Length ; findUniquePair ( a , n ) ; } }
Count unset bits of a number | An optimized C # program to count unset bits in an integer . ; This makes sure two bits ( From MSB and including MSB ) are set ; This makes sure 4 bits ( From MSB and including MSB ) are set ; Count set bits in toggled number ; To store the count of set bits ; Driver code
using System ; class GFG { static int countUnsetBits ( int n ) { int x = n ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return BitCount ( x ^ n ) ; } static int BitCount ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { int n = 17 ; Console . WriteLine ( countUnsetBits ( n ) ) ; } }
Count total bits in a number | Function to get no of bits in binary representation of positive integer ; Driver Code
using System ; class GFG { static int countBits ( int n ) { int count = 0 ; while ( n != 0 ) { count ++ ; n >>= 1 ; } return count ; } static public void Main ( ) { int i = 65 ; Console . Write ( countBits ( i ) ) ; } }
Toggle all bits after most significant bit | C # program to toggle set bits starting from MSB ; Function to toggle bits starting from MSB ; temporary variable to use XOR with one of a n ; Run loop until the only set bit in temp crosses MST of n . ; Toggle bit of n corresponding to current set bit in temp . ; Move set bit to next higher position . ; Driver code
using System ; class GFG { static int toggle ( int n ) { int temp = 1 ; while ( temp <= n ) { n = n ^ temp ; temp = temp << 1 ; } return n ; } public static void Main ( ) { int n = 10 ; n = toggle ( n ) ; Console . Write ( n ) ; } }
Find the n | C # program to find n - th number whose binary representation is palindrome . ; Finds if the kth bit is set in the binary representation ; Returns the position of leftmost set bit in the binary representation ; Finds whether the integer in binary representation is palindrome or not ; One by one compare bits ; Compare left and right bits and converge ; Start from 1 , traverse through all the integers ; If we reach n , break the loop ; Driver code ; Function Call
using System ; class GFG { static int INT_MAX = 2147483647 ; static int isKthBitSet ( int x , int k ) { return ( ( x & ( 1 << ( k - 1 ) ) ) > 0 ) ? 1 : 0 ; } static int leftmostSetBit ( int x ) { int count = 0 ; while ( x > 0 ) { count ++ ; x = x >> 1 ; } return count ; } static int isBinPalindrome ( int x ) { int l = leftmostSetBit ( x ) ; int r = 1 ; while ( l > r ) { if ( isKthBitSet ( x , l ) != isKthBitSet ( x , r ) ) return 0 ; l -- ; r ++ ; } return 1 ; } static int findNthPalindrome ( int n ) { int pal_count = 0 ; int i = 0 ; for ( i = 1 ; i <= INT_MAX ; i ++ ) { if ( isBinPalindrome ( i ) > 0 ) { pal_count ++ ; } if ( pal_count == n ) break ; } return i ; } static public void Main ( ) { int n = 9 ; Console . WriteLine ( findNthPalindrome ( n ) ) ; } }
Print ' K ' th least significant bit of a number | C # code to print ' K ' th LSB ; Function returns 1 if set , 0 if not ; Driver code ; Function call
using System ; class GFG { static bool LSB ( int num , int K ) { bool x = ( num & ( 1 << ( K - 1 ) ) ) != 0 ; return ( x ) ; } static void Main ( ) { int num = 10 , K = 4 ; if ( LSB ( num , K ) ) Console . Write ( "1" ) ; else Console . Write ( "0" ) ; } }
Check if two numbers are equal without using comparison operators | C # code to check if 2 numbers are same ; Finds if a and b are same ; Driver code
using System ; class GFG { static void areSame ( int a , int b ) { if ( ( a - b ) == 0 ) Console . Write ( " Same " ) ; else Console . Write ( " Not ▁ Same " ) ; } public static void Main ( ) { areSame ( 10 , 20 ) ; } }
Toggle bits in the given range | C # implementation to toggle bits in the given range ; Function to toggle 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 ; toggle bits in the range l to r in ' n ' Besides this , we can calculate num as : num = ( 1 << r ) - l . and return the number ; Driver Code
using System ; namespace Toggle { public class GFG { static int toggleBitsFromLToR ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return ( n ^ num ) ; } public static void Main ( ) { int n = 50 ; int l = 2 , r = 5 ; Console . Write ( toggleBitsFromLToR ( n , l , r ) ) ; } } }
Position of rightmost different bit | C # implementation to find the position of rightmost different bit ; Function to find the position of rightmost set bit in ' n ' ; to handle edge case when n = 0. ; Function to find the position of rightmost different bit in the binary representations of ' m ' and ' n ' ; position of rightmost different bit ; Driver code
using System ; class GFG { static int getRightMostSetBit ( int n ) { if ( n == 0 ) return 0 ; return ( int ) ( ( Math . Log10 ( n & - n ) ) / Math . Log10 ( 2 ) ) + 1 ; } static int posOfRightMostDiffBit ( int m , int n ) { return getRightMostSetBit ( m ^ n ) ; } public static void Main ( ) { int m = 52 , n = 4 ; Console . Write ( " Position ▁ = ▁ " + posOfRightMostDiffBit ( m , n ) ) ; } }