codeparrot/xlcost-text-to-code huggingface dataset:
Check whether all the bits are set in the given range | C # implementation to check whether all the bits are set in the given range or not ; 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 ; Driver code
using System ; 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 ( num == new_num ) return " Yes " ; return " No " ; } public static void Main ( ) { int n = 22 ; int l = 2 , r = 3 ; Console . Write ( allBitsSetInTheGivenRange ( n , l , r ) ) ; } }
1 to n bit numbers with no consecutive 1 s in binary representation | C # Code to Print all numbers upto n bits with no consecutive set bits . ; Let us first compute 2 raised to power n . ; loop 1 to n to check all the numbers ; A number i doesn ' t ▁ contain ▁ ▁ consecutive ▁ set ▁ bits ▁ if ▁ ▁ bitwise ▁ and ▁ of ▁ i ▁ and ▁ left ▁ ▁ shifted ▁ i ▁ do ' t contain a commons set bit . ; Driver code
using System ; class GFG { static void printNonConsecutive ( int n ) { int p = ( 1 << n ) ; for ( int i = 1 ; i < p ; i ++ ) if ( ( i & ( i << 1 ) ) == 0 ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int n = 3 ; printNonConsecutive ( n ) ; } }
Find the n | Efficient C # program to find n - th palindrome ; Construct the nth binary palindrome with the given group number , aux_number and operation type ; No need to insert any bit in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and  fill around middle ; Insert bit 0 in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and  fill around middle ; else Insert bit 1 in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and  fill around middle ; Convert the number to decimal from binary ; Will return the nth binary palindrome number ; Add number of elements in all the groups , until the group of the nth number is found ; Total number of elements until this group ; Element 's offset position in the group ; Finding which bit to be placed in the middle and finding the number , which we will fill from the middle in both directions ; We need to fill this auxiliary number in binary form the middle in both directions ; op = 0 ; Need to Insert 0 at middle ; op = 1 ; Need to Insert 1 at middle ; Driver code ; Function Call
using System ; class GFG { static int INT_SIZE = 32 ; static int constructNthNumber ( int group_no , int aux_num , int op ) { int [ ] a = new int [ INT_SIZE ] ; int num = 0 , len_f ; int i = 0 ; if ( op == 2 ) { len_f = 2 * group_no ; a [ len_f - 1 ] = a [ 0 ] = 1 ; while ( aux_num > 0 ) { a [ group_no + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } else if ( op == 0 ) { len_f = 2 * group_no + 1 ; a [ len_f - 1 ] = a [ 0 ] = 1 ; a [ group_no ] = 0 ; while ( aux_num > 0 ) { a [ group_no + 1 + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } { len_f = 2 * group_no + 1 ; a [ len_f - 1 ] = a [ 0 ] = 1 ; a [ group_no ] = 1 ; while ( aux_num > 0 ) { a [ group_no + 1 + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } for ( i = 0 ; i < len_f ; i ++ ) num += ( 1 << i ) * a [ i ] ; return num ; } static int getNthNumber ( int n ) { int group_no = 0 , group_offset ; int count_upto_group = 0 , count_temp = 1 ; int op , aux_num ; while ( count_temp < n ) { group_no ++ ; count_upto_group = count_temp ; count_temp += 3 * ( 1 << ( group_no - 1 ) ) ; } group_offset = n - count_upto_group - 1 ; if ( ( group_offset + 1 ) <= ( 1 << ( group_no - 1 ) ) ) { aux_num = group_offset ; } else { if ( ( ( group_offset + 1 ) - ( 1 << ( group_no - 1 ) ) ) % 2 == 1 ) else aux_num = ( ( group_offset ) - ( 1 << ( group_no - 1 ) ) ) / 2 ; } return constructNthNumber ( group_no , aux_num , op ) ; } public static void Main ( String [ ] args ) { int n = 9 ; Console . Write ( " { 0 } " , getNthNumber ( n ) ) ; } }
Shuffle a pack of cards and answer the query | C # program to find the card at given index after N shuffles ; function to find card at given index ; Answer will be reversal of N bits from MSB ; Calculating the reverse binary representation ; Printing the result ; Driver code ; No . of Shuffle Steps ; Key position
using System ; class GFG { static void shuffle ( int N , int key ) { int NO_OF_BITS = N ; int reverse_num = 0 , temp ; for ( int i = 0 ; i < NO_OF_BITS ; i ++ ) { temp = ( key & ( 1 << i ) ) ; if ( temp > 0 ) reverse_num |= ( 1 << ( ( NO_OF_BITS - 1 ) - i ) ) ; } Console . Write ( reverse_num ) ; } public static void Main ( ) { int N = 3 ; int key = 3 ; shuffle ( N , key ) ; } }
Sum of numbers with exactly 2 bits set | C # program to find sum of numbers upto n whose 2 bits are set ; To count number of set bits ; To calculate sum of numbers ; To count sum of number whose 2 bit are set ; Driver Code
using System ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( countSetBits ( i ) == 2 ) sum += i ; return sum ; } static public void Main ( ) { int n = 10 ; Console . WriteLine ( findSum ( n ) ) ; } }
Toggle the last m bits | C # implementation to toggle the last m bits ; 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 ; Driver Code
using System ; namespace Toggle { public class GFG { public static int toggleLastMBits ( int n , int m ) { int num = ( 1 << m ) - 1 ; return ( n ^ num ) ; } public static void Main ( ) { int n = 107 , m = 4 ; n = toggleLastMBits ( n , m ) ; Console . Write ( n ) ; } } }
Set the rightmost unset bit | C # implementation to set the rightmost unset bit ; Function to find the position of rightmost set bit ; If n = 0 , return 1 ; If all bits of ' n ' are set ; Position of rightmost unset bit in ' n ' passing ~ n as argument ; Set the bit at position ' pos ' ; Driver code
using System ; class GFG { static int getPosOfRightmostSetBit ( int n ) { return ( int ) ( ( Math . Log10 ( n & - n ) ) / ( Math . Log10 ( 2 ) ) ) + 1 ; } static int setRightmostUnsetBit ( int n ) { if ( n == 0 ) return 1 ; if ( ( n & ( n + 1 ) ) == 0 ) return n ; int pos = getPosOfRightmostSetBit ( ~ n ) ; return ( ( 1 << ( pos - 1 ) ) n ) ; } public static void Main ( String [ ] arg ) { int n = 21 ; Console . Write ( setRightmostUnsetBit ( n ) ) ; } }
Previous smaller integer having one less number of set bits | C # implementation to find the previous smaller integer with one less number of set bits ; function to find the position of rightmost set bit . ; function to find the previous smaller integer ; position of rightmost set bit of n ; turn off or unset the bit at position ' pos ' ; Driver code
using System ; class GFG { static int getFirstSetBitPos ( int n ) { return ( int ) ( Math . Log ( n & - n ) / Math . Log ( 2 ) ) + 1 ; } static int previousSmallerInteger ( int n ) { int pos = getFirstSetBitPos ( n ) ; return ( n & ~ ( 1 << ( pos - 1 ) ) ) ; } public static void Main ( ) { int n = 25 ; Console . WriteLine ( " Previous ▁ small ▁ Integer ▁ = " + previousSmallerInteger ( n ) ) ; } }
Check if all bits of a number are set | C # implementation to check whether every digit in the binary representation of the given number is set or not ; function to check if all the bits are set or not in the binary representation of ' n ' ; all bits are not set ; loop till n becomes '0' ; if the last bit is not set ; right shift ' n ' by 1 ; all bits are set ; Driver Code
using System ; class GFG { static String areAllBitsSet ( int n ) { if ( n == 0 ) return " No " ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) return " No " ; n = n >> 1 ; } return " Yes " ; } static public void Main ( ) { int n = 7 ; Console . WriteLine ( areAllBitsSet ( n ) ) ; } }
Check if all bits of a number are set | C # implementation to check whether every digit in the binary representation of the given number is set or not ; 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 ; Driver Code
using System ; class GFG { static String areAllBitsSet ( int n ) { if ( n == 0 ) return " No " ; if ( ( ( n + 1 ) & n ) == 0 ) return " Yes " ; return " No " ; } static public void Main ( ) { int n = 7 ; Console . WriteLine ( areAllBitsSet ( n ) ) ; } }
Next greater integer having one more number of set bits | C # implementation to find the next greater integer with one more number of set bits ; function to find the position of rightmost set bit . Returns - 1 if there are no set bits ; function to find the next greater integer ; position of rightmost unset bit of n by passing ~ n as argument ; if n consists of unset bits , then set the rightmost unset bit ; n does not consists of unset bits ; Driver code
using System ; class GFG { static int getFirstSetBitPos ( int n ) { return ( ( int ) ( Math . Log ( n & - n ) / Math . Log ( 2 ) ) + 1 ) - 1 ; } static int nextGreaterWithOneMoreSetBit ( int n ) { int pos = getFirstSetBitPos ( ~ n ) ; if ( pos > - 1 ) return ( 1 << pos ) | n ; return ( ( n << 1 ) + 1 ) ; } public static void Main ( ) { int n = 10 ; Console . Write ( " Next ▁ greater ▁ integer ▁ = ▁ " + nextGreaterWithOneMoreSetBit ( n ) ) ; } }
Count numbers whose sum with x is equal to XOR with x | C # program to count numbers whose bitwise XOR and sum with x are equal ; Function to find total 0 bit in a number ; Function to find Count of non - negative numbers less than or equal to x , whose bitwise XOR and SUM with x are equal . ; count number of zero bit in x ; power of 2 to count ; Driver code ; Function call
using System ; class GFG { static int CountZeroBit ( int x ) { int count = 0 ; while ( x > 0 ) { if ( ( x & 1 ) == 0 ) count ++ ; x >>= 1 ; } return count ; } static int CountXORandSumEqual ( int x ) { int count = CountZeroBit ( x ) ; return ( 1 << count ) ; } static public void Main ( ) { int x = 10 ; Console . WriteLine ( CountXORandSumEqual ( x ) ) ; } }
Find missing number in another array which is shuffled copy | C # implementation to find the missing number in shuffled array ; Returns the missing number Size of arr2 [ ] is n - 1 ; Missing number ' mnum ' ; 1 st array is of size ' n ' ; 2 nd array is of size ' n ▁ - ▁ 1' ; Required missing number ; Driver Code
using System ; class GFG { static int missingNumber ( int [ ] arr1 , int [ ] arr2 , int n ) { int mnum = 0 ; for ( int i = 0 ; i < n ; i ++ ) mnum = mnum ^ arr1 [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) mnum = mnum ^ arr2 [ i ] ; return mnum ; } public static void Main ( ) { int [ ] arr1 = { 4 , 8 , 1 , 3 , 7 } ; int [ ] arr2 = { 7 , 4 , 3 , 1 } ; int n = arr1 . Length ; Console . Write ( " Missing ▁ number ▁ = ▁ " + missingNumber ( arr1 , arr2 , n ) ) ; } }
Find Duplicates of array using bit array | A class to represent array of bits using array of integers ; Constructor ; Divide by 32. To store n bits , we need n / 32 + 1 integers ( Assuming int is stored using 32 bits ) ; Get value of a bit at given position ; Divide by 32 to find position of integer . ; Now find bit number in arr [ index ] ; Find value of given bit number in arr [ index ] ; Sets a bit at given position ; Find index of bit position ; Set bit number in arr [ index ] ; Main function to print all Duplicates ; create a bit with 32000 bits ; Traverse array elements ; Index in bit array ; If num is already present in bit array ; Else insert num ; Driver code
using System ; class BitArray { int [ ] arr ; public BitArray ( int n ) { arr = new int [ ( int ) ( n >> 5 ) + 1 ] ; } bool get ( int pos ) { int index = ( pos >> 5 ) ; int bitNo = ( pos & 0x1F ) ; return ( arr [ index ] & ( 1 << bitNo ) ) != 0 ; } void set ( int pos ) { int index = ( pos >> 5 ) ; int bitNo = ( pos & 0x1F ) ; arr [ index ] |= ( 1 << bitNo ) ; } static void checkDuplicates ( int [ ] arr ) { BitArray ba = new BitArray ( 320000 ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { int num = arr [ i ] ; if ( ba . get ( num ) ) Console . Write ( num + " ▁ " ) ; else ba . set ( num ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 5 , 1 , 10 , 12 , 10 } ; checkDuplicates ( arr ) ; } }
Count smaller values whose XOR with x is greater than x | C # program to find count of values whose XOR with x is greater than x and values are smaller than x ; Initialize result ; Traversing through all bits of x ; If current last bit of x is set then increment count by n . Here n is a power of 2 corresponding to position of bit ; Simultaneously calculate the 2 ^ n ; Replace x with x / 2 ; ; Driver code
using System ; class GFG { static int countValues ( int x ) { int count = 0 , n = 1 ; while ( x != 0 ) { if ( x % 2 == 0 ) count += n ; n *= 2 ; x /= 2 ; } return count ; } public static void Main ( ) { int x = 10 ; Console . Write ( countValues ( x ) ) ; } }
Construct an array from XOR of all elements of array except element at same index | C # program to construct array from XOR of elements of given array ; function to construct new array ; calculate xor of array ; update array ; Driver code ; print result
using System ; class GFG { static void constructXOR ( int [ ] A , int n ) { int XOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) XOR ^= A [ i ] ; for ( int i = 0 ; i < n ; i ++ ) A [ i ] = XOR ^ A [ i ] ; } public static void Main ( ) { int [ ] A = { 2 , 4 , 1 , 3 , 5 } ; int n = A . Length ; constructXOR ( A , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( A [ i ] + " ▁ " ) ; } }
Count all pairs of an array which differ in K bits | C # program to count all pairs with bit difference as k ; Utility function to count total ones in a number ; Function to count pairs of K different bits ; long ans = 0 ; initialize final answer ; Check for K differ bit ; Driver code
using System ; class GFG { static int bitCount ( int n ) { int count = 0 ; while ( n > 0 ) { if ( ( n & 1 ) > 0 ) ++ count ; n >>= 1 ; } return count ; } static long countPairsWithKDiff ( int [ ] arr , int n , int k ) { for ( int i = 0 ; i < n - 1 ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int xoredNum = arr [ i ] ^ arr [ j ] ; if ( k == bitCount ( xoredNum ) ) ++ ans ; } } return ans ; } public static void Main ( ) { int k = 2 ; int [ ] arr = { 2 , 4 , 1 , 3 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " + k + " ▁ are ▁ " + countPairsWithKDiff ( arr , n , k ) + " STRNEWLINE " ) ; } }
Count all pairs of an array which differ in K bits | Below is C # approach of finding total k bit difference pairs ; Function to calculate K bit different pairs in array ; Get the maximum value among all array elemensts ; Set the count array to 0 , count [ ] stores the total frequency of array elements ; Initialize result ; For 0 bit answer will be total count of same number ; if count [ i ] is 0 , skip the next loop as it will not contribute the answer ; Update answer if k differ bit found ; Driver code
using System ; using System . Linq ; class GFG { static long kBitDifferencePairs ( int [ ] arr , int n , int k ) { int MAX = arr . Max ( ) ; long [ ] count = new long [ MAX + 1 ] ; for ( int i = 0 ; i < n ; ++ i ) ++ count [ arr [ i ] ] ; long ans = 0 ; if ( k == 0 ) { for ( int i = 0 ; i <= MAX ; ++ i ) ans += ( count [ i ] * ( count [ i ] - 1 ) ) / 2 ; return ans ; } for ( int i = 0 ; i <= MAX ; ++ i ) { if ( count [ i ] == 0 ) continue ; for ( int j = i + 1 ; j <= MAX ; ++ j ) { if ( BitCount ( i ^ j ) == k ) ans += count [ i ] * count [ j ] ; } } return ans ; } static int BitCount ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } public static void Main ( String [ ] args ) { int k = 2 ; int [ ] arr = { 2 , 4 , 1 , 3 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " + k + " ▁ are ▁ = ▁ " + kBitDifferencePairs ( arr , n , k ) ) ; k = 3 ; Console . WriteLine ( " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " + k + " ▁ are ▁ = ▁ " + kBitDifferencePairs ( arr , n , k ) ) ; } }
Ways to represent a number as a sum of 1 ' s ▁ and ▁ 2' s | C # program to find number of ways to representing a number as a sum of 1 ' s ▁ and ▁ 2' s ; Function to multiply matrix . ; Power function in log n ; function that returns ( n + 1 ) th Fibonacci number Or number of ways to represent n as sum of 1 ' s ▁ ▁ 2' s ; Driver program
class GFG { static void multiply ( int [ , ] F , int [ , ] M ) { int x = F [ 0 , 0 ] * M [ 0 , 0 ] + F [ 0 , 1 ] * M [ 1 , 0 ] ; int y = F [ 0 , 0 ] * M [ 0 , 1 ] + F [ 0 , 1 ] * M [ 1 , 1 ] ; int z = F [ 1 , 0 ] * M [ 0 , 0 ] + F [ 1 , 1 ] * M [ 1 , 0 ] ; int w = F [ 1 , 0 ] * M [ 0 , 1 ] + F [ 1 , 1 ] * M [ 1 , 1 ] ; F [ 0 , 0 ] = x ; F [ 0 , 1 ] = y ; F [ 1 , 0 ] = z ; F [ 1 , 1 ] = w ; } static void power ( int [ , ] F , int n ) { if ( n == 0 n == 1 ) { return ; } int [ , ] M = { { 1 , 1 } , { 1 , 0 } } ; power ( F , n / 2 ) ; multiply ( F , F ) ; if ( n % 2 != 0 ) { multiply ( F , M ) ; } } static int countWays ( int n ) { int [ , ] F = { { 1 , 1 } , { 1 , 0 } } ; if ( n == 0 ) { return 0 ; } power ( F , n ) ; return F [ 0 , 0 ] ; } public static void Main ( ) { int n = 5 ; System . Console . WriteLine ( countWays ( n ) ) ; } }
Multiplication of two numbers with shift operator | C # program to find multiplication of two number without use of multiplication operator ; Function for multiplication ; check for set bit and left shift n , count times ; increment of place value ( count ) ; Driver Code
using System ; class GFG { static int multiply ( int n , int m ) { int ans = 0 , count = 0 ; while ( m > 0 ) { if ( m % 2 == 1 ) ans += n << count ; count ++ ; m /= 2 ; } return ans ; } public static void Main ( ) { int n = 20 , m = 13 ; Console . WriteLine ( multiply ( n , m ) ) ; } }
Compare two integers without using any Comparison operator | C # program to compare two integers without any comparison operator . ; function return true if A ^ B > 0 else false ; Driver Code
using System ; class GFG { static bool EqualNumber ( int A , int B ) { if ( ( A ^ B ) > 0 ) return true ; else return false ; } public static void Main ( ) { int A = 5 , B = 6 ; if ( ! EqualNumber ( A , B ) == false ) Console . WriteLine ( "0" ) ; else Console . WriteLine ( "1" ) ; } }
Check if bits of a number has count of consecutive set bits in increasing order | C # program to check if counts of consecutive 1 s are increasing order . ; Returns true if n has counts of consecutive 1 's are increasing  order. ; Initialize previous count ; We traverse bits from right to left and check if counts are decreasing order . ; Ignore 0 s until we reach a set bit . ; Count current set bits ; Compare current with previous and update previous . ; Driver code
using System ; class GFG { static bool areSetBitsIncreasing ( int n ) { int prev_count = int . MaxValue ; while ( n > 0 ) { while ( n > 0 && n % 2 == 0 ) n = n / 2 ; int curr_count = 1 ; while ( n > 0 && n % 2 == 1 ) { n = n / 2 ; curr_count ++ ; } if ( curr_count >= prev_count ) return false ; prev_count = curr_count ; } return true ; } static public void Main ( ) { int n = 10 ; if ( areSetBitsIncreasing ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Check if a number has bits in alternate pattern | Set 1 | Program to find if a number has alternate bit pattern ; Returns true if n has alternate bit pattern else returns false ; Store last bit ; Traverse through remaining bits ; If current bit is same as previous ; Driver method
using System ; class Test { static bool findPattern ( int n ) { int prev = n % 2 ; n = n / 2 ; while ( n > 0 ) { int curr = n % 2 ; if ( curr == prev ) return false ; prev = curr ; n = n / 2 ; } return true ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( findPattern ( n ) ? " Yes " : " No " ) ; } }
XOR counts of 0 s and 1 s in binary representation | C # program to find XOR of counts 0 s and 1 s in binary representation of n . ; Returns XOR of counts 0 s and 1 s in binary representation of n . ; calculating count of zeros and ones ; Driver Program
using System ; class GFG { static int countXOR ( int n ) { int count0 = 0 , count1 = 0 ; while ( n != 0 ) { if ( n % 2 == 0 ) count0 ++ ; else count1 ++ ; n /= 2 ; } return ( count0 ^ count1 ) ; } public static void Main ( ) { int n = 31 ; Console . WriteLine ( countXOR ( n ) ) ; } }
Count all pairs with given XOR | C # program to Count all pair with given XOR value x ; Returns count of pairs in arr [ 0. . n - 1 ] with XOR value equals to x . ; create empty map that stores counts of individual elements of array . ; If there exist an element in map m with XOR equals to x ^ arr [ i ] , that means there exist an element such that the XOR of element with arr [ i ] is equal to x , then increment count . ; Increment count of current element ; return total count of pairs with XOR equal to x ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int xorPairCount ( int [ ] arr , int n , int x ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int curr_xor = x ^ arr [ i ] ; if ( m . ContainsKey ( curr_xor ) ) result += m [ curr_xor ] ; if ( m . ContainsKey ( arr [ i ] ) ) { var val = m [ arr [ i ] ] ; m . Remove ( arr [ i ] ) ; m . Add ( arr [ i ] , val + 1 ) ; } else { m . Add ( arr [ i ] , 1 ) ; } } return result ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 5 , 2 } ; int n = arr . Length ; int x = 0 ; Console . WriteLine ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ XOR ▁ = ▁ " + xorPairCount ( arr , n , x ) ) ; } }
Bitwise and ( or & ) of a range | An efficient C # program to find bit - wise & of all numbers from x to y . ; Find position of MSB in n . For example if n = 17 , then position of MSB is 4. If n = 7 , value of MSB is 3 ; Function to find Bit - wise & of all numbers from x to y . ; Initialize result ; Find positions of MSB in x and y ; If positions are not same , return ; Add 2 ^ msb_p1 to result ; subtract 2 ^ msb_p1 from x and y . ; Driver code
using System ; class GFG { static int msbPos ( long n ) { int msb_p = - 1 ; while ( n > 0 ) { n = n >> 1 ; msb_p ++ ; } return msb_p ; } static long andOperator ( long x , long y ) { long res = 0 ; while ( x > 0 && y > 0 ) { int msb_p1 = msbPos ( x ) ; int msb_p2 = msbPos ( y ) ; if ( msb_p1 != msb_p2 ) break ; long msb_val = ( 1 << msb_p1 ) ; res = res + msb_val ; x = x - msb_val ; y = y - msb_val ; } return res ; } public static void Main ( ) { long x = 10 , y = 15 ; Console . WriteLine ( andOperator ( x , y ) ) ; } }
Multiply a number with 10 without using multiplication operator | C # Code to Multiply a number with 10 without using multiplication operator ; Function to find multiplication of n with 10 without using multiplication operator ; Driver Code
using System ; class GFG { public static int multiplyTen ( int n ) { return ( n << 1 ) + ( n << 3 ) ; } public static void Main ( ) { int n = 50 ; Console . Write ( multiplyTen ( n ) ) ; } }
Equal Sum and XOR | C # program to print count of values such that n + i = n ^ i ; function to count number of values less than equal to n that satisfy the given condition ; Traverse all numbers from 0 to n and increment result only when given condition is satisfied . ; Driver program to test above function
using System ; class GFG { public static int countValues ( int n ) { int countV = 0 ; for ( int i = 0 ; i <= n ; i ++ ) if ( ( n + i ) == ( n ^ i ) ) countV ++ ; return countV ; } public static void Main ( ) { int n = 12 ; Console . WriteLine ( countValues ( n ) ) ; } }