codeparrot/xlcost-text-to-code huggingface dataset:
Count of subarrays of size K with elements having even frequencies | C # program to count subarrays of size K with all elements having even frequencies ; Function to return count of required subarrays ; If K is odd ; Not possible to have any such subarrays ; Stores the starting index of every subarrays ; Stores the count of required subarrays ; Stores Xor of the current subarray . ; Xor of first subarray of size K ; If all elements appear even number of times , increase the count of such subarrays ; Remove the starting element from the current subarray ; Traverse the array for the remaining subarrays ; Update Xor by adding the last element of the current subarray ; Increment i ; If currXor becomes 0 , then increment count ; Update currXor by removing the starting element of the current subarray ; Return count ; Driver Code
using System ; class GFG { static int countSubarray ( int [ ] arr , int K , int N ) { if ( K % 2 != 0 ) return 0 ; if ( N < K ) return 0 ; int start = 0 ; int i = 0 ; int count = 0 ; int currXor = arr [ i ++ ] ; while ( i < K ) { currXor ^= arr [ i ] ; i ++ ; } if ( currXor == 0 ) count ++ ; currXor ^= arr [ start ++ ] ; while ( i < N ) { currXor ^= arr [ i ] ; i ++ ; if ( currXor == 0 ) count ++ ; currXor ^= arr [ start ++ ] ; } return count ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 4 , 2 , 2 , 4 } ; int K = 4 ; int N = arr . Length ; Console . Write ( countSubarray ( arr , K , N ) ) ; } }
Count of even and odd set bit Array elements after XOR with K for Q queries | C # program to count number of even and odd set bits elements after XOR with a given element ; Store the count of set bits ; Brian Kernighan 's algorithm ; Function to solve Q queries ; Store set bits in X ; Count set bits of X ; Driver code
using System ; class GFG { static int even , odd ; static void keep_count ( int [ ] arr , int N ) { int count ; for ( int i = 0 ; i < N ; i ++ ) { count = 0 ; while ( arr [ i ] != 0 ) { arr [ i ] = ( arr [ i ] - 1 ) & arr [ i ] ; count ++ ; } if ( count % 2 == 0 ) even ++ ; else odd ++ ; } return ; } static void solveQueries ( int [ ] arr , int n , int [ ] q , int m ) { even = 0 ; odd = 0 ; keep_count ( arr , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int X = q [ i ] ; int count = 0 ; while ( X != 0 ) { X = ( X - 1 ) & X ; count ++ ; } if ( count % 2 == 0 ) { Console . Write ( even + " ▁ " + odd + " STRNEWLINE " ) ; } else { Console . Write ( odd + " ▁ " + even + " STRNEWLINE " ) ; } } } public static void Main ( ) { int [ ] arr = { 2 , 7 , 4 , 5 , 3 } ; int n = arr . Length ; int [ ] q = { 3 , 4 , 12 , 6 } ; int m = q . Length ; solveQueries ( arr , n , q , m ) ; } }
Range Queries for finding the Sum of all even parity numbers | C # program to find the sum of all Even Parity numbers in the given range ; pref [ ] array to precompute the sum of all Even Parity Numbers ; Function that returns true if count of set bits in x is even ; Parity will store the count of set bits ; Function to precompute the sum of all even parity numbers upto 100000 ; isEvenParity ( ) return the number i if i has even parity else return 0 ; Function to print sum for each query ; Function to print sum of all even parity numbers between [ L , R ] ; Function that pre computes the sum of all even parity numbers ; Iterate over all Queries to print sum ; Driver code ; Queries ; Function that print the sum of all even parity numbers in Range [ L , R ]
using System ; class GFG { static int [ ] pref = new int [ 100001 ] ; static int isEvenParity ( int num ) { int parity = 0 ; int x = num ; while ( x != 0 ) { if ( ( x & 1 ) == 1 ) parity ++ ; x = x >> 1 ; } if ( parity % 2 == 0 ) return num ; else return 0 ; } static void preCompute ( ) { for ( int i = 1 ; i < 100001 ; i ++ ) { pref [ i ] = pref [ i - 1 ] + isEvenParity ( i ) ; } } static void printSum ( int L , int R ) { Console . WriteLine ( pref [ R ] - pref [ L - 1 ] ) ; } static void printSum ( int [ , ] arr , int Q ) { preCompute ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i , 0 ] , arr [ i , 1 ] ) ; } } public static void Main ( ) { int N = 2 ; int [ , ] Q = { { 1 , 10 } , { 121 , 211 } } ; printSum ( Q , N ) ; } }
Largest number in the Array having frequency same as value | C # solution to the above problem ; Function to find the largest number whose frequency is equal to itself . ; Find the maximum element in the array ; Driver code
using System ; using System . Linq ; class GFG { static int findLargestNumber ( int [ ] arr ) { int k = arr . Max ( ) ; int [ ] m = new int [ k + 1 ] ; foreach ( int n in arr ) ++ m [ n ] ; for ( int n = arr . Length - 1 ; n > 0 ; -- n ) { if ( n == m [ n ] ) return n ; } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 5 , 2 , 4 , 5 } ; Console . Write ( findLargestNumber ( arr ) + " STRNEWLINE " ) ; } }
Largest number in the Array having frequency same as value | C # code for the above problem ; Function to find the largest number whose frequency is equal to itself . ; Adding 65536 to keep the count of the current number ; Right shifting by 16 bits to find the count of the number i ; Driver code
using System ; class GFG { static int findLargestNumber ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] &= 0xFFFF ; if ( arr [ i ] <= n ) { arr [ i ] += 0x10000 ; } } for ( int i = n - 1 ; i > 0 ; -- i ) { if ( ( arr [ i ] >> 16 ) == i ) return i + 1 ; } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 5 , 5 , 2 , 4 , 5 } ; int n = arr . Length ; Console . Write ( findLargestNumber ( arr , n ) + " STRNEWLINE " ) ; } }
Convert numbers into binary representation and add them without carry | C # program to add two binary number without carry ; Function returns sum of both the binary number without carry ; XOR of N and M ; Driver code
using System ; class GFG { static int NoCarrySum ( int N , int M ) { return N ^ M ; } static public void Main ( String [ ] args ) { int N = 37 ; int M = 12 ; Console . Write ( NoCarrySum ( N , M ) ) ; } }
Check if all the set bits of the binary representation of N are at least K places away | C # program to check if all the set bits of the binary representation of N are at least K places away . ; Initialize check and count with 0 ; The i - th bit is a set bit ; This is the first set bit so , start calculating all the distances between consecutive bits from here ; If count is less than K return false ; Adding the count as the number of zeroes increase between set bits ; Driver code
using System ; public class GFG { static bool CheckBits ( int N , int K ) { int check = 0 ; int count = 0 ; for ( int i = 31 ; i >= 0 ; i -- ) { if ( ( ( 1 << i ) & N ) > 0 ) { if ( check == 0 ) { check = 1 ; } else { if ( count < K ) { return false ; } } count = 0 ; } else { count ++ ; } } return true ; } static public void Main ( ) { int N = 5 ; int K = 1 ; if ( CheckBits ( N , K ) ) { Console . Write ( " YES " ) ; } else { Console . Write ( " NO " ) ; } } }
Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z | C # program to minimize number of bits to be fipped in X or Y such that their bitwise or is equal to minimize ; This function returns minimum number of bits to be flipped in X and Y to make X | Y = Z ; If current bit in Z is set and is also set in either of X or Y or both ; If current bit in Z is set and is unset in both X and Y ; Set that bit in either X or Y ; If current bit in Z is unset and is set in both X and Y ; Unset the bit in both X and Y ; If current bit in Z is unset and is set in either X or Y ; Unset that set bit ; Driver Code
using System ; class GFG { static int minimumFlips ( int X , int Y , int Z ) { int res = 0 ; while ( X > 0 Y > 0 Z > 0 ) { if ( ( ( X % 2 == 1 ) || ( Y % 2 == 1 ) ) && ( Z % 2 == 1 ) ) { X = X >> 1 ; Y = Y >> 1 ; Z = Z >> 1 ; continue ; } else if ( ! ( X % 2 == 1 ) && ! ( Y % 2 == 1 ) && ( Z % 2 == 1 ) ) { res ++ ; } else if ( ( X % 2 == 1 ) || ( Y % 2 == 1 ) ) { if ( ( X % 2 == 1 ) && ( Y % 2 == 1 ) && ! ( Z % 2 == 1 ) ) { res += 2 ; } else if ( ( ( X % 2 == 1 ) || ( Y % 2 == 1 ) ) && ! ( Z % 2 == 1 ) ) { res ++ ; } } X = X >> 1 ; Y = Y >> 1 ; Z = Z >> 1 ; } return res ; } public static void Main ( ) { int X = 5 , Y = 8 , Z = 6 ; Console . Write ( minimumFlips ( X , Y , Z ) ) ; } }
Count subsequences with same values of Bitwise AND , OR and XOR | function for finding count of possible subsequence ; creating a map to count the frequency of each element ; store frequency of each element ; iterate through the map ; add all possible combination for key equal zero ; add all ( odd number of elements ) possible combination for key other than zero ; Driver function
static int countSubseq ( int [ ] arr , int n ) { int count = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { var val = mp [ arr [ i ] ] ; mp . Remove ( arr [ i ] ) ; mp . Add ( arr [ i ] , val + 1 ) ; } else { mp . Add ( arr [ i ] , 1 ) ; } } foreach ( KeyValuePair < int , int > entry in mp ) { if ( entry . Key == 0 ) count += ( int ) ( Math . Pow ( 2 , entry . Value - 1 ) ) ; else count += ( int ) ( Math . Pow ( 2 , entry . Value - 1 ) ) ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , 2 , 5 , 6 } ; int n = arr . Length ; Console . WriteLine ( countSubseq ( arr , n ) ) ; } }
Choose an integer K such that maximum of the xor values of K with all Array elements is minimized | C # implementation to find minimum possible value of the maximum xor in an array by choosing some integer ; Function to calculate minimum possible value of the maximum XOR in an array ; Base case ; Divide elements into two sections ; Traverse all elements of current section and divide in two groups ; Check if one of the sections is empty ; Explore both the possibilities using recursion ; Function to calculate minimum XOR value ; Start recursion from the most significant pos position ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int calculate ( List < int > section , int pos ) { if ( pos < 0 ) return 0 ; List < int > on_section = new List < int > ( ) , off_section = new List < int > ( ) ; foreach ( int el in section ) { if ( ( ( el >> pos ) & 1 ) == 0 ) off_section . Add ( el ) ; else on_section . Add ( el ) ; } if ( off_section . Count == 0 ) return calculate ( on_section , pos - 1 ) ; if ( on_section . Count == 0 ) return calculate ( off_section , pos - 1 ) ; return Math . Min ( calculate ( off_section , pos - 1 ) , calculate ( on_section , pos - 1 ) ) + ( 1 << pos ) ; } static int minXorValue ( int [ ] a , int n ) { List < int > section = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) section . Add ( a [ i ] ) ; return calculate ( section , 30 ) ; } public static void Main ( String [ ] args ) { int N = 4 ; int [ ] A = { 3 , 2 , 5 , 6 } ; Console . Write ( minXorValue ( A , N ) ) ; } }
Program to find the Nth natural number with exactly two bits set | C # Code to find the Nth number with exactly two bits set ; Function to find the Nth number with exactly two bits set ; Keep incrementing until we reach the partition of ' N ' stored in bit_L ; set the rightmost bit based on bit_R ; Driver code
using System ; class GFG { static void findNthNum ( int N ) { int bit_L = 1 , last_num = 0 ; while ( bit_L * ( bit_L + 1 ) / 2 < N ) { last_num = last_num + bit_L ; bit_L ++ ; } int bit_R = N - last_num - 1 ; Console . Write ( ( 1 << bit_L ) + ( 1 << bit_R ) + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int N = 13 ; findNthNum ( N ) ; } }
XOR of all Prime numbers in an Array at positions divisible by K | C # program to find XOR of all Prime numbers in an Array at positions divisible by K ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to find the required XOR ; To store XOR of the primes ; Traverse the array ; If the number is a prime ; If index is divisible by k ; Print the xor ; Driver code ; Function call
using System ; class GFG { static int MAX = 1000005 ; static bool [ ] prime = new bool [ MAX ] ; static void SieveOfEratosthenes ( bool [ ] prime ) { prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } } static void prime_xor ( int [ ] arr , int n , int k ) { for ( int i = 0 ; i < MAX ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { if ( ( i + 1 ) % k == 0 ) { ans ^= arr [ i ] ; } } } Console . WriteLine ( ans ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 7 , 11 , 8 } ; int n = arr . Length ; int K = 2 ; prime_xor ( arr , n , K ) ; } }
Find XOR of all elements in an Array | C # program to find the XOR of all elements in the array ; Function to find the XOR of all elements in the array ; Resultant variable ; Iterating through every element in the array ; Find XOR with the result ; Return the XOR ; Driver Code ; Function call
using System ; class GFG { static int xorOfArray ( int [ ] arr , int n ) { int xor_arr = 0 ; for ( int i = 0 ; i < n ; i ++ ) { xor_arr = xor_arr ^ arr [ i ] ; } return xor_arr ; } public static void Main ( string [ ] args ) { int [ ] arr = { 3 , 9 , 12 , 13 , 15 } ; int n = arr . Length ; Console . WriteLine ( xorOfArray ( arr , n ) ) ; } }
Count of even set bits between XOR of two arrays | C # program for the above approach ; Function that count the XOR of [ ] B with all the element in [ ] A having even set bit ; Count the set bits in A [ i ] ; check for even or Odd ; To store the count of element for [ ] B such that XOR with all the element in [ ] A having even set bit ; Count set bit for B [ i ] ; check for Even or Odd ; Driver Code
using System ; class GFG { static void countEvenBit ( int [ ] A , int [ ] B , int n , int m ) { int i , cntOdd = 0 , cntEven = 0 ; for ( i = 0 ; i < n ; i ++ ) { int x = bitCount ( A [ i ] ) ; if ( x % 2 == 1 ) { cntEven ++ ; } else { cntOdd ++ ; } } int [ ] CountB = new int [ m ] ; for ( i = 0 ; i < m ; i ++ ) { int x = bitCount ( B [ i ] ) ; if ( x % 2 == 1 ) { CountB [ i ] = cntEven ; } else { CountB [ i ] = cntOdd ; } } for ( i = 0 ; i < m ; i ++ ) { Console . Write ( CountB [ i ] + " ▁ " ) ; } } static int bitCount ( int x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { int [ ] A = { 4 , 2 , 15 , 9 , 8 , 8 } ; int [ ] B = { 3 , 4 , 22 } ; countEvenBit ( A , B , 6 , 3 ) ; } }
Check if a Number is Odd or Even using Bitwise Operators | C # program to check for even or odd using Bitwise XOR operator ; Returns true if n is even , else odd ; n ^ 1 is n + 1 , then even , else odd ; Driver code
using System ; class GFG { static bool isEven ( int n ) { if ( ( n ^ 1 ) == n + 1 ) return true ; else return false ; } public static void Main ( String [ ] args ) { int n = 100 ; Console . Write ( isEven ( n ) == true ? " Even " : " Odd " ) ; } }
Bitwise OR ( | ) of all even number from 1 to N | C # implementation of the above approach ; Function to return the bitwise OR of all the even numbers upto N ; Initialize result as 2 ; Driver code
using System ; class GFG { static int bitwiseOrTillN ( int n ) { int result = 2 ; for ( int i = 4 ; i <= n ; i = i + 2 ) { result = result | i ; } return result ; } static public void Main ( ) { int n = 10 ; Console . WriteLine ( bitwiseOrTillN ( n ) ) ; } }
Bitwise OR ( | ) of all even number from 1 to N | C # implementation of the above approach ; Function to return the bitwise OR of all even numbers upto N ; For value less than 2 ; Count total number of bits in bitwise or all bits will be set except last bit ; Compute 2 to the power bitCount and subtract 2 ; Driver code
using System ; class GFG { static int bitwiseOrTillN ( int n ) { if ( n < 2 ) return 0 ; int bitCount = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) + 1 ; return ( int ) Math . Pow ( 2 , bitCount ) - 2 ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( bitwiseOrTillN ( n ) ) ; } }
Generating N | C # program Generating N - bit Gray Code starting from K ; Function to Generating N - bit Gray Code starting from K ; Generate gray code of corresponding binary number of integer i . ; Driver code
using System ; class GFG { static void genSequence ( int n , int val ) { for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int x = i ^ ( i >> 1 ) ^ val ; Console . Write ( x + " ▁ " ) ; } } public static void Main ( ) { int n = 3 , k = 2 ; genSequence ( n , k ) ; } }
Winner in the Rock | C # implementation of the approach ; Function to return the winner of the game ; Both the players chose to play the same move ; Player A wins the game ; Function to perform the queries ; Driver code
using System ; using System . Collections . Generic ; class GFG { static String winner ( String moves ) { Dictionary < char , int > data = new Dictionary < char , int > ( ) ; data . Add ( ' R ' , 0 ) ; data . Add ( ' P ' , 1 ) ; data . Add ( ' S ' , 2 ) ; if ( moves [ 0 ] == moves [ 1 ] ) { return " Draw " ; } if ( ( ( data [ moves [ 0 ] ] | 1 << ( 2 ) ) - ( data [ moves [ 1 ] ] | 0 << ( 2 ) ) ) % 3 != 0 ) { return " A " ; } return " B " ; } static void performQueries ( String [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( winner ( arr [ i ] ) + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String [ ] arr = { " RS " , " SR " , " SP " , " PP " } ; int n = arr . Length ; performQueries ( arr , n ) ; } }
Maximum number of consecutive 1 's in binary representation of all the array elements | C # implementation of the approach ; Function to return the count of maximum consecutive 1 s in the binary representation of x ; Initialize result ; Count the number of iterations to reach x = 0. ; This operation reduces length of every sequence of 1 s by one ; Function to return the count of maximum consecutive 1 s in the binary representation among all the elements of arr [ ] ; To store the answer ; For every element of the array ; Count of maximum consecutive 1 s in the binary representation of the current element ; Update the maximum count so far ; Driver code
using System ; class GFG { static int maxConsecutiveOnes ( int x ) { int count = 0 ; while ( x != 0 ) { x = ( x & ( x << 1 ) ) ; count ++ ; } return count ; } static int maxOnes ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int currMax = maxConsecutiveOnes ( arr [ i ] ) ; ans = Math . Max ( ans , currMax ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( maxOnes ( arr , n ) ) ; } }
Maximum Bitwise OR pair from a range | C # implementation of the approach ; Function to return the maximum bitwise OR possible among all the possible pairs ; Check for every possible pair ; Maximum among all ( i , j ) pairs ; Driver code
using System ; class GFG { static int maxOR ( int L , int R ) { int maximum = int . MinValue ; for ( int i = L ; i < R ; i ++ ) for ( int j = i + 1 ; j <= R ; j ++ ) maximum = Math . Max ( maximum , ( i j ) ) ; return ; } public static void ( String [ ] args ) { int L = 4 , R = 5 ; Console . WriteLine ( maxOR ( L , R ) ) ; } }
Maximum Bitwise OR pair from a range | C # implementation of the above approach ; Function to return the maximum bitwise OR possible among all the possible pairs ; If there is only a single value in the range [ L , R ] ; Loop through each bit from MSB to LSB ; MSBs where the bits differ , all bits from that bit are set ; If MSBs are same , then ans bit is same as that of bit of right or left limit ; Driver code
using System ; class GFG { static int MAX = 64 ; static int maxOR ( int L , int R ) { if ( L == R ) { return L ; } int ans = 0 ; for ( int i = MAX - 1 ; i >= 0 ; i -- ) { int p , lbit , rbit ; p = 1 << i ; if ( ( rbit == 1 ) && ( lbit == 0 ) ) { ans += ( p << 1 ) - 1 ; break ; } if ( rbit == 1 ) { ans += p ; } } return ans ; } public static void Main ( String [ ] args ) { int L = 4 , R = 5 ; Console . WriteLine ( maxOR ( L , R ) ) ; } }
Longest subsequence with a given AND value | O ( N ) | C # implementation of the approach ; Function to return the required length ; To store the filtered numbers ; Filtering the numbers ; If there are no elements to check ; Find the AND of all the filtered elements ; Check if the AND is equal to m ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findLen ( int [ ] arr , int n , int m ) { List < int > filter = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & m ) == m ) filter . Add ( arr [ i ] ) ; if ( filter . Count == 0 ) return 0 ; int c_and = filter [ 0 ] ; for ( int i = 1 ; i < filter . Count ; i ++ ) c_and &= filter [ i ] ; if ( c_and == m ) return filter . Count ; return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 3 , 3 , 1 , 3 } ; int n = arr . Length ; int m = 3 ; Console . WriteLine ( findLen ( arr , n , m ) ) ; } }
Longest sub | C # implementation of the approach ; Function to return the required length ; To store the filtered numbers ; Filtering the numbers ; If there are no elements to check ; Find the OR of all the filtered elements ; Check if the OR is equal to m ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findLen ( int [ ] arr , int n , int m ) { List < int > filter = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] m ) == m ) filter . Add ( arr [ i ] ) ; if ( filter . Count == 0 ) return 0 ; int c_or = filter [ 0 ] ; for ( int i = 1 ; i < filter . Count ; i ++ ) c_or |= filter [ i ] ; if ( c_or == m ) return filter . Count ; return 0 ; } public static void Main ( ) { int [ ] arr = { 7 , 3 , 3 , 1 , 3 } ; int n = arr . Length ; int m = 3 ; Console . Write ( findLen ( arr , n , m ) ) ; } }
Program to toggle K | C # program to toggle K - th bit of a number N ; Function to toggle the kth bit of n ; Driver code
using System ; class GFG { static int toggleBit ( int n , int k ) { return ( n ^ ( 1 << ( k - 1 ) ) ) ; } public static void Main ( String [ ] args ) { int n = 5 , k = 2 ; Console . WriteLine ( " { 0 } " , toggleBit ( n , k ) ) ; } }
Program to clear K | C # program to clear K - th bit of a number N ; Function to clear the kth bit of n ; Driver code
using System ; class GFG { static int clearBit ( int n , int k ) { return ( n & ( ~ ( 1 << ( k - 1 ) ) ) ) ; } public static void Main ( String [ ] args ) { int n = 5 , k = 1 ; Console . WriteLine ( clearBit ( n , k ) ) ; } }
Maximize the Expression | Bit Manipulation | C # implementation of the above approach ; Function to return the value of the maximized expression ; int can have 32 bits ; Consider the ith bit of D to be 1 ; Calculate the value of ( B AND bitOfD ) ; Check if bitOfD satisfies ( B AND D = D ) ; Check if bitOfD can maximize ( A XOR D ) ; Note that we do not need to consider ith bit of D to be 0 because if above condition are not satisfied then value of result will not change which is similar to considering bitOfD = 0 as result XOR 0 = result ; Driver code
using System ; class GFG { static int MAX = 32 ; static int maximizeExpression ( int a , int b ) { int result = a ; for ( int bit = MAX - 1 ; bit >= 0 ; bit -- ) { int bitOfD = 1 << bit ; int x = b & bitOfD ; if ( x == bitOfD ) { int y = result & bitOfD ; if ( y == 0 ) { result = result ^ bitOfD ; } } } return result ; } public static void Main ( String [ ] args ) { int a = 11 , b = 14 ; Console . WriteLine ( maximizeExpression ( a , b ) ) ; } }