codeparrot/xlcost-text-to-code huggingface dataset:
Left Shift and Right Shift Operators in C / C ++ |  ; shift y by 61 bits left
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int x = 19 ; unsigned long long y = 19 ; cout << " x ▁ < < ▁ 1 ▁ = ▁ " << ( x << 1 ) << endl ; cout << " x ▁ > > ▁ 1 ▁ = ▁ " << ( x >> 1 ) << endl ; cout << " y ▁ < < ▁ 61 ▁ = ▁ " << ( y << 61 ) << endl ; return 0 ; }
Left Shift and Right Shift Operators in C / C ++ |
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int i = 3 ; cout << " pow ( 2 , ▁ " << i << " ) ▁ = ▁ " << ( 1 << i ) << endl ; i = 4 ; cout << " pow ( 2 , ▁ " << i << " ) ▁ = ▁ " << ( 1 << i ) << endl ; return 0 ; }
Range query for count of set bits | C ++ program to Range query for Count number of set bits ; 2 - D array that will stored the count of bits set in element of array ; Function store the set bit count in BitCount Array ; traverse over all bits ; mark elements with i 'th bit set ; Check whether the current bit is set or not if it 's set then mark it. ; store cumulative sum of bits ; Function to process queries ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int BitCount [ 10000 ] = { 0 } ; void fillSetBitsMatrix ( int arr [ ] , int n ) { for ( int i = 0 ; i < 32 ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { long temp = arr [ j ] >> i ; if ( temp % 2 != 0 ) BitCount [ j ] += 1 ; } } for ( int i = 1 ; i < n ; i ++ ) BitCount [ i ] += BitCount [ i - 1 ] ; } void Query ( int Q [ ] [ 2 ] , int q ) { for ( int i = 0 ; i < q ; i ++ ) cout << ( BitCount [ Q [ i ] [ 1 ] ] - BitCount [ Q [ i ] [ 0 ] - 1 ] ) << endl ; } int main ( ) { int Arr [ ] = { 1 , 5 , 6 , 10 , 9 , 4 , 67 } ; int n = sizeof ( Arr ) / sizeof ( Arr [ 0 ] ) ; fillSetBitsMatrix ( Arr , n ) ; int q = 2 ; int Q [ 2 ] [ 2 ] = { { 1 , 5 } , { 2 , 6 } } ; Query ( Q , q ) ; return 0 ; }
Generate n | C ++ program to generate n - bit gray codes ; Function to convert decimal to binary ; leftmost digits are filled with 0 ; Function to generate gray code ; generate gray code of corresponding binary number of integer i . ; printing gray code ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void decimalToBinaryNumber ( int x , int n ) { int * binaryNumber = new int ( x ) ; int i = 0 ; while ( x > 0 ) { binaryNumber [ i ] = x % 2 ; x = x / 2 ; i ++ ; } for ( int j = 0 ; j < n - i ; j ++ ) cout << '0' ; for ( int j = i - 1 ; j >= 0 ; j -- ) cout << binaryNumber [ j ] ; } void generateGrayarr ( int n ) { int N = 1 << n ; for ( int i = 0 ; i < N ; i ++ ) { int x = i ^ ( i >> 1 ) ; decimalToBinaryNumber ( x , n ) ; cout << endl ; } } int main ( ) { int n = 3 ; generateGrayarr ( n ) ; return 0 ; }
Sum of bitwise AND of all possible subsets of given set | C ++ program to calculate sum of Bit - wise and sum of all subsets of an array ; assuming representation of each element is in 32 bit ; iterating array element ; Counting the set bit of array in ith position ; counting subset which produce sum when particular bit position is set . ; multiplying every position subset with 2 ^ i to count the sum . ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define BITS  32 NEW_LINE int andSum ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < BITS ; i ++ ) { int countSetBits = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] & ( 1 << i ) ) countSetBits ++ ; } int subset = ( 1 << countSetBits ) - 1 ; subset = ( subset * ( 1 << i ) ) ; ans += subset ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << andSum ( arr , size ) ; return 0 ; }
Maximize the number by rearranging bits | An efficient C ++ program to find minimum number formed by bits of a given number . ; Returns maximum number formed by bits of a given number . ; _popcnt32 ( a ) gives number of 1 's  present in binary representation of a. ; If all 32 bits are set . ; find a number witn n least significant set bits . ; Now shift result by 32 - n ; Driver function .
#include <bits/stdc++.h> NEW_LINE #define ll  unsigned int NEW_LINE using namespace std ; ll maximize ( ll a ) { ll n = _popcnt32 ( a ) ; if ( n == 32 ) return a ; ll res = ( 1 << n ) - 1 ; return ( res << ( 32 - n ) ) ; } int main ( ) { ll a = 3 ; cout << maximize ( a ) << endl ; return 0 ; }
Minimum number using set bits of a given number | An efficient C ++ program to find minimum number formed by bits of a given number . ; Returns minimum number formed by bits of a given number . ; _popcnt32 ( a ) gives number of 1 's  present in binary representation  of a. ; Driver function .
#include <bits/stdc++.h> NEW_LINE #define ll  unsigned int NEW_LINE using namespace std ; ll minimize ( ll a ) { ll n = _popcnt32 ( a ) ; return ( pow ( 2 , n ) - 1 ) ; } int main ( ) { ll a = 11 ; cout << minimize ( a ) << endl ; return 0 ; }
Disjoint Set Union on trees | Set 2 | CPP code to find maximum possible cost ; Edge structure ; v : Adjacency list representation of Graph p : stores parents of nodes ; Weighted union - find with path compression ; DFS is called to generate parent of a node from adjacency list representation ; Utility function for Union ; Fixed ' i ' as AND ; Generating supermasks of ' i ' ; Checking whether p [ x ] is also a supermask of i . ; Keep track of maximum size of subtree ; Storing maximum cost of subtree with a given AND ; Separating components which are merged during Union operation for next AND value . ; Driver code ; Number of nodes ; Taking edges as input and put them in adjacency list representation ; Initializing parent vertex of '1' as '1' ; Call DFS to generate ' p ' array
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100010 NEW_LINE struct Edge { int u , v ; } ; vector < int > v [ N ] ; int p [ N ] ; struct wunionfind { int id [ N ] , sz [ N ] ; void initial ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) id [ i ] = i , sz [ i ] = 1 ; } int Root ( int idx ) { int i = idx ; while ( i != id [ i ] ) id [ i ] = id [ id [ i ] ] , i = id [ i ] ; return i ; } void Union ( int a , int b ) { int i = Root ( a ) , j = Root ( b ) ; if ( i != j ) { if ( sz [ i ] >= sz [ j ] ) { id [ j ] = i , sz [ i ] += sz [ j ] ; sz [ j ] = 0 ; } else { id [ i ] = j , sz [ j ] += sz [ i ] ; sz [ i ] = 0 ; } } } } ; wunionfind W ; void dfs ( int u , int parent ) { for ( int i = 0 ; i < v [ u ] . size ( ) ; i ++ ) { int j = v [ u ] [ i ] ; if ( j != parent ) { p [ j ] = u ; dfs ( j , u ) ; } } } int UnionUtil ( int n ) { int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int maxi = 1 ; for ( int x = i ; x <= n ; x = ( i | ( x + 1 ) ) ) { int y = p [ x ] ; if ( ( y & i ) == i ) { W . Union ( x , y ) ; maxi = max ( maxi , W . sz [ W . Root ( x ) ] ) ; } } ans = max ( ans , maxi * i ) ; for ( int x = i ; x <= n ; x = ( i | ( x + 1 ) ) ) { W . sz [ x ] = 1 ; W . id [ x ] = x ; } } return ans ; } int main ( ) { int n , i ; n = 6 ; W . initial ( n ) ; Edge e [ ] = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } , { 3 , 5 } , { 5 , 6 } } ; int q = sizeof ( e ) / sizeof ( e [ 0 ] ) ; for ( i = 0 ; i < q ; i ++ ) { int x , y ; x = e [ i ] . u , y = e [ i ] . v ; v [ x ] . push_back ( y ) ; v [ y ] . push_back ( x ) ; } p [ 1 ] = 1 ; dfs ( 1 , -1 ) ; int ans = UnionUtil ( n ) ; printf ( " Maximum ▁ Cost ▁ = ▁ % d STRNEWLINE " , ans ) ; return 0 ; }
Maximum steps to transform 0 to X with bitwise AND | CPP code to find the maximum possible effort ; Function to get no of set bits in binary representation of positive integer n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int countSetBits ( unsigned int n ) { unsigned int count = 0 ; while ( n ) { count += n & 1 ; n >>= 1 ; } return count ; } int main ( ) { int i = 3 ; cout << countSetBits ( i ) ; return 0 ; }
Check a number is odd or even without modulus operator | A simple C ++ program to check for even or odd ; Returns true if n is even , else odd ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isEven ( int n ) { bool isEven = true ; for ( int i = 1 ; i <= n ; i ++ ) isEven = ! isEven ; return isEven ; } int main ( ) { int n = 101 ; isEven ( n ) ? cout << " Even " : cout << " Odd " ; return 0 ; }
Check a number is odd or even without modulus operator | A simple C ++ program to check for even or odd ; Returns true if n is even , else odd ; Return true if n / 2 does not result in a float value . ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isEven ( int n ) { return ( ( n / 2 ) * 2 == n ) ; } int main ( ) { int n = 101 ; isEven ( n ) ? cout << " Even " : cout << " Odd " ; return 0 ; }
Count pairs in an array which have at least one digit common | CPP Program to count pairs in an array with some common digit ; Returns true if the pair is valid , otherwise false ; converting integers to strings ; Iterate over the strings and check if a character in first string is also present in second string , return true ; No common digit found ; Returns the number of valid pairs ; Iterate over all possible pairs ; Driver Code to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkValidPair ( int num1 , int num2 ) { string s1 = to_string ( num1 ) ; string s2 = to_string ( num2 ) ; for ( int i = 0 ; i < s1 . size ( ) ; i ++ ) for ( int j = 0 ; j < s2 . size ( ) ; j ++ ) if ( s1 [ i ] == s2 [ j ] ) return true ; return false ; } int countPairs ( int arr [ ] , int n ) { int numberOfPairs = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( checkValidPair ( arr [ i ] , arr [ j ] ) ) numberOfPairs ++ ; return numberOfPairs ; } int main ( ) { int arr [ ] = { 10 , 12 , 24 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , n ) << endl ; return 0 ; }
Check if bitwise AND of any subset is power of two | CPP Program to check if Bitwise AND of any subset is power of two ; Check for power of 2 or not ; Check if there exist a subset whose bitwise AND is power of 2. ; if there is only one element in the set . ; Finding a number with all bit sets . ; check all the positions at which the bit is set . ; include all those elements whose i - th bit is set ; check for the set contains elements make a power of 2 or not ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int NUM_BITS = 32 ; bool isPowerOf2 ( int num ) { return ( num && ! ( num & ( num - 1 ) ) ) ; } bool checkSubsequence ( int arr [ ] , int n ) { if ( n == 1 ) return isPowerOf2 ( arr [ 0 ] ) ; int total = 0 ; for ( int i = 0 ; i < NUM_BITS ; i ++ ) total = total | ( 1 << i ) ; for ( int i = 0 ; i < NUM_BITS ; i ++ ) { int ans = total ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] & ( 1 << i ) ) ans = ans & arr [ j ] ; } if ( isPowerOf2 ( ans ) ) return true ; } return false ; } int main ( ) { int arr [ ] = { 12 , 13 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( checkSubsequence ( arr , n ) ) printf ( " YES STRNEWLINE " ) ; else printf ( " NO STRNEWLINE " ) ; return 0 ; }
Levelwise Alternating OR and XOR operations in Segment Tree | C ++ program to build levelwise OR / XOR alternating Segment tree ; A utility function to get the middle index from corner indexes . ; A recursive function that constructs Segment Tree for array [ ss . . se ] . si is index of current node in segment tree st operation denotes which operation is carried out at that level to merge the left and right child . It 's either 0 or 1. ; If there is one element in array , store it in current node of segment tree and return ; If there are more than one elements , then recur for left and right subtrees and store the sum of values in this node ; Build the left and the right subtrees by using the fact that operation at level ( i + 1 ) = ! ( operation at level i ) ; merge the left and right subtrees by checking the operation to be carried . If operation = 1 , then do OR else XOR ; OR operation ; XOR operation ; Function to construct segment tree from given array . This function allocates memory for segment tree and calls constructSTUtil ( ) to fill the allocated memory ; Height of segment tree ; Maximum size of segment tree ; Allocate memory ; operation = 1 ( XOR ) if Height of tree is even else it is 0 ( OR ) for the root node ; Fill the allocated memory st ; Return the constructed segment tree ; Driver Code ; leaf nodes ; Build the segment tree ; Root node is at index 0 considering 0 - based indexing in segment Tree ; print value at rootIndex
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMid ( int s , int e ) { return s + ( e - s ) / 2 ; } void constructSTUtil ( int arr [ ] , int ss , int se , int * st , int si , int operation ) { if ( ss == se ) { st [ si ] = arr [ ss ] ; return ; } int mid = getMid ( ss , se ) ; constructSTUtil ( arr , ss , mid , st , si * 2 + 1 , ! operation ) ; constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 2 , ! operation ) ; if ( operation == 1 ) { st [ si ] = ( st [ 2 * si + 1 ] st [ 2 * si + 2 ] ) ; } else { st [ si ] = ( st [ 2 * si + 1 ] ^ st [ 2 * si + 2 ] ) ; } } int * constructST ( int arr [ ] , int n ) { int x = ( int ) ( ceil ( log2 ( n ) ) ) ; int max_size = 2 * ( int ) pow ( 2 , x ) - 1 ; int * st = new int [ max_size ] ; int operationAtRoot = ( x % 2 == 0 ? 0 : 1 ) ; constructSTUtil ( arr , 0 , n - 1 , st , 0 , operationAtRoot ) ; return st ; } int main ( ) { int leaves [ ] = { 1 , 6 , 3 , 7 , 5 , 9 , 10 , 4 } ; int n = sizeof ( leaves ) / sizeof ( leaves [ 0 ] ) ; int * segmentTree = constructST ( leaves , n ) ; int rootIndex = 0 ; cout << " Value ▁ at ▁ Root ▁ Node ▁ = ▁ " << segmentTree [ rootIndex ] ; }
Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs | CPP program to print the Leftover element after performing alternate Bitwise OR and Bitwise XOR operations to the pairs . ; count the step number ; if one element is there , it will be the answer ; at first step we do a bitwise OR ; keep on doing bitwise operations till the last element is left ; perform operations ; if step is the odd step ; else even step ; answer when one element is left ; Driver Code ; 1 st query ; 2 nd query
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  1000 NEW_LINE int lastElement ( int a [ ] , int n ) { int steps = 1 ; vector < int > v [ N ] ; if ( n == 1 ) return a [ 0 ] ; for ( int i = 0 ; i < n ; i += 2 ) v [ steps ] . push_back ( a [ i ] a [ i + 1 ] ) ; while ( v [ steps ] . size ( ) > 1 ) { steps += 1 ; for ( int i = 0 ; i < v [ steps - 1 ] . size ( ) ; i += 2 ) { if ( steps & 1 ) v [ steps ] . push_back ( v [ steps - 1 ] [ i ] v [ steps - 1 ] [ i + 1 ] ) ; v [ steps ] . push_back ( v [ steps - 1 ] [ i ] ^ v [ steps - 1 ] [ i + 1 ] ) ; } } return v [ steps ] [ 0 ] ; } int main ( ) { int a [ ] = { 1 , 4 , 5 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int index = 0 ; int value = 2 ; a [ 0 ] = 2 ; cout << lastElement ( a , n ) << endl ; index = 3 ; value = 5 ; a [ index ] = value ; cout << lastElement ( a , n ) << endl ; return 0 ; }
Find the winner in nim | CPP to find nim - game winner ; function to find winner of NIM - game ; case when Alice is winner ; when Bob is winner ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findWinner ( int A [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res ^= A [ i ] ; if ( res == 0 n % 2 == 0 ) return " Alice " ; else return " Bob " ; } int main ( ) { int A [ ] = { 1 , 4 , 3 , 5 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << " Winner ▁ = ▁ " << findWinner ( A , n ) ; return 0 ; }
Fibbinary Numbers ( No consecutive 1 s in binary ) | C ++ implementation to check whether a number is fibbinary or not ; function to check whether a number is fibbinary or not ; if the number does not contain adjacent ones then ( n & ( n >> 1 ) ) operation results to 0 ; not a fibbinary number ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isFibbinaryNum ( unsigned int n ) { if ( ( n & ( n >> 1 ) ) == 0 ) return true ; return false ; } int main ( ) { unsigned int n = 10 ; if ( isFibbinaryNum ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Maximum XOR | Program to obtain maximum XOR value sub - array ; function to calculate maximum XOR value ; Return ( 2 ^ c - 1 ) ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxXOR ( int n , int k ) { int c = log2 ( n ) + 1 ; return ( ( 1 << c ) - 1 ) ; } int main ( ) { int n = 12 ; int k = 3 ; cout << maxXOR ( n , k ) ; return 0 ; }
Divide two integers without using multiplication , division and mod operator | C ++ implementation to Divide two integers without using multiplication , division and mod operator ; Function to divide a by b and return floor value it ; Calculate sign of divisor i . e . , sign will be negative only iff either one of them is negative otherwise it will be positive ; remove sign of operands ; Initialize the quotient ; test down from the highest bit and accumulate the tentative value for valid bit ; if the sign value computed earlier is - 1 then negate the value of quotient ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divide ( long long dividend , long long divisor ) { int sign = ( ( dividend < 0 ) ^ ( divisor < 0 ) ) ? -1 : 1 ; dividend = abs ( dividend ) ; divisor = abs ( divisor ) ; long long quotient = 0 , temp = 0 ; for ( int i = 31 ; i >= 0 ; -- i ) { if ( temp + ( divisor << i ) <= dividend ) { temp += divisor << i ; quotient |= 1LL << i ; } } if ( sign == -1 ) quotient = - quotient ; return quotient ; } int main ( ) { int a = 10 , b = 3 ; cout << divide ( a , b ) << " STRNEWLINE " ; a = 43 , b = -8 ; cout << divide ( a , b ) ; return 0 ; }
XOR of two numbers after making length of their binary representations equal | C ++ implementation to return XOR of two numbers after making length of their binary representation same ; function to count the number of bits in binary representation of an integer ; initialize count ; count till n is non zero ; right shift by 1 i . e , divide by 2 ; function to calculate the xor of two numbers by adding trailing zeros to the number having less number of bits in its binary representation . ; stores the minimum and maximum ; left shift if the number of bits are less in binary representation ; driver code to check the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int n ) { int c = 0 ; while ( n ) { c ++ ; n = n >> 1 ; } return c ; } int XOR ( int a , int b ) { int c = min ( a , b ) ; int d = max ( a , b ) ; if ( count ( c ) < count ( d ) ) c = c << ( count ( d ) - count ( c ) ) ; return ( c ^ d ) ; } int main ( ) { int a = 13 , b = 5 ; cout << XOR ( a , b ) ; return 0 ; }
Check if binary string multiple of 3 using DFA | C ++ program to check if the binary string is divisible by 3. ; Function to check if the binary string is divisible by 3. ; checking if the bit is nonzero ; checking if the nonzero bit is at even position ; Checking if the difference of non - zero oddbits and evenbits is divisible by 3. ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void CheckDivisibilty ( string A ) { int oddbits = 0 , evenbits = 0 ; for ( int counter = 0 ; counter < A . length ( ) ; counter ++ ) { if ( A [ counter ] == '1' ) { if ( counter % 2 == 0 ) { evenbits ++ ; } else { oddbits ++ ; } } } if ( abs ( oddbits - evenbits ) % 3 == 0 ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } } int main ( ) { string A = "10101" ; CheckDivisibilty ( A ) ; return 0 ; }
Swap every two bits in bytes | C ++ program to swap every two bits in a byte . ; Extracting the high bit shift it to lowbit Extracting the low bit shift it to highbit ; Driver function to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int swapBitsInPair ( unsigned int x ) { return ( ( x & 0b10101010 ) >> 1 ) | ( ( x & 0b01010101 ) << 1 ) ; } int main ( ) { unsigned int x = 4 ; cout << swapBitsInPair ( x ) ; return 0 ; }
Alternate bits of two numbers to create a new number | CPP Program to generate a number using alternate bits of two numbers . ; set even bit of number n ; res for store 101010. . number ; generate number form of 101010. . ... till temp size ; if bit is even then generate number and or with res ; return set even bit number ; set odd bit of number m ; res for store 101010. . number ; generate number form of 101010. . . . till temp size ; if bit is even then generate number and or with res ; return set odd bit number ; set even bit of number n ; set odd bit of number m ; take OR with these number ; Driver code ; n = 1 0 1 0 ^ ^ m = 1 0 1 1 ^ ^ result = 1 0 1 1
#include <iostream> NEW_LINE using namespace std ; int setevenbits ( int n ) { int temp = n ; int count = 0 ; int res = 0 ; for ( temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 1 ) res |= ( 1 << count ) ; count ++ ; } return ( n & res ) ; } int setoddbits ( int m ) { int count = 0 ; int res = 0 ; for ( int temp = m ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 0 ) res |= ( 1 << count ) ; count ++ ; } return ( m & res ) ; } int getAlternateBits ( int n , int m ) { int tempn = setevenbits ( n ) ; int tempm = setoddbits ( m ) ; return ( tempn tempm ) ; } int main ( ) { int n = 10 ; int m = 11 ; cout << getAlternateBits ( n , m ) ; return 0 ; }
Decimal representation of given binary string is divisible by 20 or not | C ++ implementation to check whether decimal representation of given binary number is divisible by 20 or not ; function to check whether decimal representation of given binary number is divisible by 10 or not ; if last digit is '1' , then number is not divisible by 10 ; to accumulate the sum of last digits in perfect powers of 2 ; traverse from the 2 nd last up to 1 st digit in ' bin ' ; if digit in '1' ; calculate digit 's position from  the right ; according to the digit 's position,  obtain the last digit of the  applicable perfect power of 2 ; if last digit is 0 , then divisible by 10 ; not divisible by 10 ; function to check whether decimal representation of given binary number is divisible by 20 or not ; if ' bin ' is an odd number ; check if bin ( 0. . n - 2 ) is divisible by 10 or not ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivisibleBy10 ( char bin [ ] , int n ) { if ( bin [ n - 1 ] == '1' ) return false ; int sum = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( bin [ i ] == '1' ) { int posFromRight = n - i - 1 ; if ( posFromRight % 4 == 1 ) sum = sum + 2 ; else if ( posFromRight % 4 == 2 ) sum = sum + 4 ; else if ( posFromRight % 4 == 3 ) sum = sum + 8 ; else if ( posFromRight % 4 == 0 ) sum = sum + 6 ; } } if ( sum % 10 == 0 ) return true ; return false ; } bool isDivisibleBy20 ( char bin [ ] , int n ) { if ( bin [ n - 1 ] == '1' ) return false ; return isDivisibleBy10 ( bin , n - 1 ) ; } int main ( ) { char bin [ ] = "101000" ; int n = sizeof ( bin ) / sizeof ( bin [ 0 ] ) ; if ( isDivisibleBy20 ( bin , n - 1 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
For every set bit of a number toggle bits of other | A CPP program toggle bits of n2 that are at same position as set bits of n1 . ; function for the Nega_bit ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int toggleBits ( int n1 , int n2 ) { return n1 ^ n2 ; } int main ( ) { int n1 = 2 , n2 = 5 ; cout << toggleBitst ( n1 , n2 ) << endl ; return 0 ; }
Toggle all even bits of a number | CPP code to Toggle all even bit of a number ; Returns a number which has all even bits of n toggled . ; Generate number form of 101010 . . till of same order as n ; if bit is even then generate number and or with res ; return toggled number ; Driver code
#include <iostream> NEW_LINE using namespace std ; int evenbittogglenumber ( int n ) { int res = 0 , count = 0 ; for ( int temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 1 ) res |= ( 1 << count ) ; count ++ ; } return n ^ res ; } int main ( ) { int n = 11 ; cout << evenbittogglenumber ( n ) ; return 0 ; }