codeparrot/xlcost-text-to-code huggingface dataset:
Number of leading zeros in binary representation of a given number | C ++ program of number of leading zeros in binary representation of a given number ; Function to count the no . of leading zeros ; Keep shifting x by one until leftmost bit does not become 1. ; Main function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countZeros ( unsigned int x ) { int total_bits = sizeof ( x ) * 8 ; int res = 0 ; while ( ! ( x & ( 1 << ( total_bits - 1 ) ) ) ) { x = ( x << 1 ) ; res ++ ; } return res ; } int main ( ) { int x = 101 ; cout << countZeros ( x ) ; return 0 ; }
Number of leading zeros in binary representation of a given number | C ++ program of number of leading zeros in binary representation of a given number ; Function to count the no . of leading zeros ; Main function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countZeros ( int x ) { unsigned y ; int n = 32 ; y = x >> 16 ; if ( y != 0 ) { n = n - 16 ; x = y ; } y = x >> 8 ; if ( y != 0 ) { n = n - 8 ; x = y ; } y = x >> 4 ; if ( y != 0 ) { n = n - 4 ; x = y ; } y = x >> 2 ; if ( y != 0 ) { n = n - 2 ; x = y ; } y = x >> 1 ; if ( y != 0 ) return n - 2 ; return n - x ; } int main ( ) { int x = 101 ; cout << countZeros ( x ) ; return 0 ; }
Comparing leading zeros in binary representations of two numbers | CPP program to find the number with more leading zeroes . ; Function to compare the no . of leading zeros ; if both have same no . of leading zeros ; if y has more leading zeros ; Main Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void LeadingZeros ( int x , int y ) { if ( ( x ^ y ) <= ( x & y ) ) cout << " Equal " else if ( ( x & ( ~ y ) ) > y ) cout << y ; else cout << x ; } int main ( ) { int x = 10 , y = 16 ; LeadingZeros ( x , y ) ; return 0 ; }
Count distinct Bitwise OR of all Subarrays | C ++ implementation of the above approach ; function to calculate count of distinct bitwise OR of all subarrays . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distintBitwiseOR ( int arr [ ] , int n ) { unordered_set < int > ans , prev ; for ( int i = 0 ; i < n ; i ++ ) { unordered_set < int > ne ; for ( auto x : prev ) ne . insert ( arr [ i ] x ) ; ne . insert ( arr [ i ] ) ; for ( auto x : ne ) ans . insert ( x ) ; prev = ne ; } return ans . size ( ) ; } int main ( ) { int n = 3 ; int arr [ ] = { 1 , 2 , 4 } ; cout << distintBitwiseOR ( arr , n ) ; return 0 ; }
Printing all subsets of { 1 , 2 , 3 , ... n } without using array or loop | C ++ code to print all subsets of { 1 , 2 , 3 , n } without using array or loop , just recursion . ; This recursive function calls subset function to print the subsets one by one . numBits -- > number of bits needed to represent the number ( simply input value n ) . num -- > Initially equal to 2 ^ n - 1 and decreases by 1 every recursion until 0. ; Print the subset corresponding to binary representation of num . ; Call the function recursively to print the next subset . ; This function recursively prints the subset corresponding to the binary representation of num . nthBit -- > nth bit from right side starting from n and decreases until 0 ; Print number in given subset only if the bit corresponding to it is set in num . ; Check for the next bit ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void subset ( int , int , int ) ; void printSubsets ( int numOfBits , int num ) { if ( num >= 0 ) { cout << " { ▁ " ; subset ( numOfBits - 1 , num , numOfBits ) ; cout << " } " << endl ; printSubsets ( numOfBits , num - 1 ) ; } else return ; } void subset ( int nthBit , int num , int numOfBits ) { if ( nthBit >= 0 ) { if ( num & ( 1 << nthBit ) ) { cout << numOfBits - nthBit << " ▁ " ; } subset ( nthBit - 1 , num , numOfBits ) ; } else return ; } int main ( ) { int n = 4 ; printSubsets ( n , pow ( 2 , n ) - 1 ) ; }
Number of mismatching bits in the binary representation of two integers | C ++ implementation of the approach ; compute number of different bits ; since , the numbers are less than 2 ^ 31 run the loop from '0' to '31' only ; right shift both the numbers by ' i ' and check if the bit at the 0 th position is different ; Driver code ; find number of different bits
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int A , int B ) { int count = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( ( A >> i ) & 1 ) != ( ( B >> i ) & 1 ) ) { count ++ ; } } cout << " Number ▁ of ▁ different ▁ bits ▁ : ▁ " << count << endl ; } int main ( ) { int A = 12 , B = 15 ; solve ( A , B ) ; return 0 ; }
Set the rightmost off bit | CPP program to set the rightmost unset bit ; If all bits are set ; Set rightmost 0 bit ; Driver program to test above
#include <iostream> NEW_LINE using namespace std ; int setRightmostUnsetBit ( int n ) { if ( ( n & ( n + 1 ) ) == 0 ) return n ; return n | ( n + 1 ) ; } int main ( ) { int n = 21 ; cout << setRightmostUnsetBit ( n ) ; return 0 ; }
Print the number of set bits in each node of a Binary Tree | CPP program to print the number of set bits in each node of the binary tree ; Binary Tree node ; Utility function that allocates a new Node ; Function to print the number of set bits in each node of the binary tree ; Print the number of set bits of current node using __builtin_popcount ( ) ; Traverse Left Subtree ; Traverse Right Subtree ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; Node * newNode ( int data ) { Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } void printSetBit ( Node * root ) { if ( root == NULL ) return ; cout << " Set ▁ bits ▁ in ▁ Node ▁ " << root -> data << " ▁ = ▁ " << __builtin_popcount ( root -> data ) << " STRNEWLINE " ; printSetBit ( root -> left ) ; printSetBit ( root -> right ) ; } int main ( ) { Node * root = newNode ( 16 ) ; root -> left = newNode ( 13 ) ; root -> left -> left = newNode ( 14 ) ; root -> left -> right = newNode ( 12 ) ; root -> right = newNode ( 11 ) ; root -> right -> left = newNode ( 10 ) ; root -> right -> right = newNode ( 16 ) ; printSetBit ( root ) ; return 0 ; }
Find bitwise AND ( & ) of all possible sub | C ++ program to find of all the sub - arrays ; function to return AND of sub - arrays ; Driver program ; size of the array ; print and of all subarrays
#include <bits/stdc++.h> NEW_LINE using namespace std ; int AND ( int a [ ] , int n ) { int ans = a [ 0 ] ; for ( int i = 0 ; i < n ; ++ i ) ans &= a [ i ] ; return ans ; } int main ( ) { int a [ ] = { 1 , 2 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << AND ( a , n ) ; return 0 ; }
2 's complement for a givin string using XOR | C ++ program to find 2 's complement using XOR. ; A flag used to find if a 1 bit is seen or not . ; xor operator is used to flip the ; bits after converting in to ASCII values ; if there is no 1 in the string so just add 1 in starting of string and return ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string TwoscomplementbyXOR ( string str ) { int n = str . length ( ) ; bool check_bit = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == '0' && check_bit == 0 ) { continue ; } else { if ( check_bit == 1 ) str [ i ] = ( str [ i ] - '0' ) ^ 1 + '0' ; check_bit = 1 ; } } if ( check_bit == 0 ) return "1" + str ; else return str ; } int main ( ) { string str = "101" ; cout << TwoscomplementbyXOR ( str ) ; return 0 ; }
Check whether bits are in alternate pattern in the given range | Set | C ++ implementation to check whether bits are in alternate pattern in the given range ; function to check whether rightmost kth bit is set or not in ' n ' ; function to set the rightmost kth bit in ' n ' ; kth bit of n is being set by this operation ; function to check if all the bits are set or not in the binary representation of ' n ' ; if true , then all bits are set ; else all bits are not set ; function to check if a number has bits in alternate pattern ; to check if all bits are set in ' num ' ; function to check whether bits are in alternate pattern in the given range ; preparing a number ' num ' and ' left _ shift ' which can be further used for the check of alternate pattern in the given range ; unset all the bits which are left to the rth bit of ( r + 1 ) th bit ; right shift ' num ' by ( l - 1 ) bits ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isKthBitSet ( unsigned int n , unsigned int k ) { if ( ( n >> ( k - 1 ) ) & 1 ) return true ; return false ; } unsigned int setKthBit ( unsigned int n , unsigned int k ) { return ( ( 1 << ( k - 1 ) ) n ) ; } bool allBitsAreSet ( unsigned int n ) { if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } bool bitsAreInAltOrder ( unsigned int n ) { unsigned int num = n ^ ( n >> 1 ) ; return allBitsAreSet ( num ) ; } bool bitsAreInAltPatrnInGivenRange ( unsigned int n , unsigned int l , unsigned int r ) { unsigned int num , left_shift ; if ( isKthBitSet ( n , r ) ) { num = n ; left_shift = r ; } else { num = setKthBit ( n , ( r + 1 ) ) ; left_shift = r + 1 ; } num = num & ( ( 1 << left_shift ) - 1 ) ; num = num >> ( l - 1 ) ; return bitsAreInAltOrder ( num ) ; } int main ( ) { unsigned int n = 18 ; unsigned int l = 1 , r = 3 ; if ( bitsAreInAltPatrnInGivenRange ( n , l , r ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check whether bits are in alternate pattern in the given range | C ++ implementation to check whether bits are in alternate pattern in the given range ; function to check whether bits are in alternate pattern in the given range ; right shift n by ( l - 1 ) bits ; get the bit at the last position in ' num ' ; right shift ' num ' by 1 ; loop until there are bits in the given range ; get the bit at the last position in ' num ' ; if true , then bits are not in alternate pattern ; update ' prev ' ; right shift ' num ' by 1 ; bits are in alternate pattern in the given range ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool bitsAreInAltPatrnInGivenTRange ( unsigned int n , unsigned int l , unsigned int r ) { unsigned int num , prev , curr ; num = n >> ( l - 1 ) ; prev = num & 1 ; num = num >> 1 ; for ( int i = 1 ; i <= ( r - l ) ; i ++ ) { curr = num & 1 ; if ( curr == prev ) return false ; prev = curr ; num = num >> 1 ; } return true ; } int main ( ) { unsigned int n = 18 ; unsigned int l = 1 , r = 3 ; if ( bitsAreInAltPatrnInGivenTRange ( n , l , r ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Increment a number without using ++ or + | CPP program to increment an unsigned int using bitwise operators . ; function that increment the value . ; Invert bits and apply negative sign ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int increment ( unsigned int i ) { i = - ( ~ i ) ; return i ; } int main ( ) { int n = 3 ; cout << increment ( n ) ; return 0 ; }
First element greater than or equal to X in prefix sum of N numbers using Binary Lifting | CPP program to find lower_bound of x in prefix sums array using binary lifting . ; function to make prefix sums array ; function to find lower_bound of x in prefix sums array using binary lifting . ; initialize position ; find log to the base 2 value of n . ; if x less than first number . ; starting from most significant bit . ; if value at this position less than x then updateposition Here ( 1 << i ) is similar to 2 ^ i . ; + 1 because ' pos ' will have position of largest value less than ' x ' ; Driver code ; given array ; value to find ; size of array ; to store prefix sum ; call for prefix sum ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MakePreSum ( int arr [ ] , int presum [ ] , int n ) { presum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) presum [ i ] = presum [ i - 1 ] + arr [ i ] ; } int BinaryLifting ( int presum [ ] , int n , int x ) { int pos = 0 ; int LOGN = log2 ( n ) ; if ( x <= presum [ 0 ] ) return 0 ; for ( int i = LOGN ; i >= 0 ; i -- ) { if ( pos + ( 1 << i ) < n && presum [ pos + ( 1 << i ) ] < x ) { pos += ( 1 << i ) ; } } return pos + 1 ; } int main ( ) { int arr [ ] = { 2 , 5 , 7 , 1 , 6 , 9 , 12 , 4 , 6 } ; int x = 8 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int presum [ n ] = { 0 } ; MakePreSum ( arr , presum , n ) ; cout << BinaryLifting ( presum , n , x ) ; return 0 ; }
Maximum sum by adding numbers with same number of set bits | C ++ program to find maximum sum by adding numbers with same number of set bits ; count the number of bits for each element of array ; Count the number of set bits ; Function to return the the maximum sum ; Calculate the ; Assuming the number to be a maximum of 32 bits ; Add the number to the number of set bits ; Find the maximum sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int bit_count ( int n ) { int count = 0 ; while ( n ) { count ++ ; n = n & ( n - 1 ) ; } return count ; } int maxsum ( int arr [ ] , int n ) { int bits [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { bits [ i ] = bit_count ( arr [ i ] ) ; } int sum [ 32 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { sum [ bits [ i ] ] += arr [ i ] ; } int maximum = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { maximum = max ( sum [ i ] , maximum ) ; } return maximum ; } int main ( ) { int arr [ ] = { 2 , 3 , 8 , 5 , 6 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxsum ( arr , n ) ; return 0 ; }
Sum of XOR of sum of all pairs in an array | CPP program to find XOR of pair sums . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorPairSum ( int ar [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum ^ ar [ i ] ; return 2 * sum ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << xorPairSum ( arr , n ) ; return 0 ; }
Count pairs with Bitwise OR as Even number | C ++ program to count pairs with even OR ; Count total even numbers in array . ; return count of even pair ; Driver main
#include <iostream> NEW_LINE using namespace std ; int findEvenPair ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( ! ( A [ i ] & 1 ) ) count ++ ; return count * ( count - 1 ) / 2 ; } int main ( ) { int A [ ] = { 5 , 6 , 2 , 8 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << findEvenPair ( A , N ) << endl ; return 0 ; }
Check whether all the bits are unset in the given range | C ++ implementation to check whether all the bits are unset in the given range or not ; function to check whether all the bits are unset in the given range or not ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; new number which could only have one or more set bits in the range l to r and nowhere else ; if true , then all bits are unset in the given range ; else all bits are not unset in the given range ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool allBitsSetInTheGivenRange ( unsigned int n , unsigned int l , unsigned int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; int new_num = n & num ; if ( new_num == 0 ) return true ; return false ; } int main ( ) { unsigned int n = 17 ; unsigned int l = 2 , r = 4 ; if ( allBitsSetInTheGivenRange ( n , l , r ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if a number has same number of set and unset bits | C ++ program to check if a number has same number of set and unset bits ; Function to check if a number has same setbits and unset bits ; iterate for all bits of a number ; if set ; if unset ; right shift number by 1 ; is number of set bits are equal to unset bits ; Driver Code ; function to check
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkSame ( int n ) { int set = 0 , unset = 0 ; while ( n ) { if ( n & 1 ) set ++ ; else unset ++ ; n = n >> 1 ; } if ( set == unset ) return true ; else return false ; } int main ( ) { int n = 12 ; if ( checkSame ( n ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Check if concatenation of two strings is balanced or not | CPP program to check if sequence obtained by concatenating two bracket sequences is balanced or not . ; Check if given string is balanced bracket sequence or not . ; If current bracket is an opening bracket push it to stack . ; If current bracket is a closing bracket then pop from stack if it is not empty . If stack is empty then sequence is not balanced . ; If stack is not empty , then sequence is not balanced . ; Function to check if string obtained by concatenating two bracket sequences is balanced or not . ; Check if s1 + s2 is balanced or not . ; Check if s2 + s1 is balanced or not . ; Driver code .
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isBalanced ( string s ) { stack < char > st ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ( ' ) st . push ( s [ i ] ) ; else { if ( st . empty ( ) ) { return false ; } else st . pop ( ) ; } } if ( ! st . empty ( ) ) return false ; return true ; } bool isBalancedSeq ( string s1 , string s2 ) { if ( isBalanced ( s1 + s2 ) ) return true ; return isBalanced ( s2 + s1 ) ; } int main ( ) { string s1 = " ) ( ) ( ( ) ) ) ) " ; string s2 = " ( ( ) ( ( ) ( " ; if ( isBalancedSeq ( s1 , s2 ) ) cout << " Balanced " ; else cout << " Not ▁ Balanced " ; return 0 ; }
Find iâ €™ th index character in a binary string obtained after n iterations | Set 2 | C ++ program to find iaTMth Index character in a binary string obtained after n iterations ; Function to find the i - th character ; distance between two consecutive elements after N iterations ; binary representation of M ; kth digit will be derived from root for sure ; Check whether there is need to flip root or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void KthCharacter ( int m , int n , int k ) { int distance = pow ( 2 , n ) ; int Block_number = k / distance ; int remaining = k % distance ; int s [ 32 ] , x = 0 ; for ( ; m > 0 ; x ++ ) { s [ x ] = m % 2 ; m = m / 2 ; } int root = s [ x - 1 - Block_number ] ; if ( remaining == 0 ) { cout << root << endl ; return ; } bool flip = true ; while ( remaining > 1 ) { if ( remaining & 1 ) { flip = ! flip ; } remaining = remaining >> 1 ; } if ( flip ) { cout << ! root << endl ; } else { cout << root << endl ; } } int main ( ) { int m = 5 , k = 5 , n = 3 ; KthCharacter ( m , n , k ) ; return 0 ; }
Check whether the number has only first and last bits set | Set 2 | C ++ to check whether the number has only first and last bits set ; function to check whether the number has only first and last bits set ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool onlyFirstAndLastAreSet ( unsigned int n ) { if ( n == 1 ) return true ; if ( n == 2 ) return false ; return ( ( ( n - 1 ) & ( n - 2 ) ) == 0 ) ; } int main ( ) { unsigned int n = 9 ; if ( onlyFirstAndLastAreSet ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Number with set bits only between L | CPP program to print the integer with all the bits set in range L - R Naive Approach ; Function to return the integer with all the bits set in range L - R ; iterate from L to R and add all powers of 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getInteger ( int L , int R ) { int number = 0 ; for ( int i = L ; i <= R ; i ++ ) number += pow ( 2 , i ) ; return number ; } int main ( ) { int L = 2 , R = 5 ; cout << getInteger ( L , R ) ; return 0 ; }
Number with set bits only between L | CPP program to print the integer with all the bits set in range L - R Efficient Approach ; Function to return the integer with all the bits set in range L - R ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int setbitsfromLtoR ( int L , int R ) { return ( 1 << ( R + 1 ) ) - ( 1 << L ) ; } int main ( ) { int L = 2 , R = 5 ; cout << setbitsfromLtoR ( L , R ) ; return 0 ; }
XOR of Sum of every possible pair of an array | C ++ program to find XOR of sum of every possible pairs in an array ; Function to find XOR of sum of all pairs ; Calculate xor of all the elements ; Return twice of xor value ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findXor ( int arr [ ] , int n ) { int xoR = 0 ; for ( int i = 0 ; i < n ; i ++ ) { xoR = xoR ^ arr [ i ] ; } return xoR * 2 ; } int main ( ) { int arr [ 3 ] = { 1 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findXor ( arr , n ) ; return 0 ; }
Largest set with bitwise OR equal to n | CPP Program to find the largest set with bitwise OR equal to n ; function to find the largest set with bitwise OR equal to n ; If the bitwise OR of n and i is equal to n , then include i in the set ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void setBitwiseORk ( int n ) { vector < int > v ; for ( int i = 0 ; i <= n ; i ++ ) { if ( ( i n ) == n ) v . push_back ( i ) ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) cout << v [ i ] << ' ▁ ' ; } int main ( ) { int n = 5 ; setBitwiseORk ( n ) ; return 0 ; }
Two odd occurring elements in an array where all other occur even times | CPP code to find two odd occurring elements in an array where all other elements appear even number of times . ; Find XOR of all numbers ; Find a set bit in the XOR ( We find rightmost set bit here ) ; Traverse through all numbers and divide them in two groups ( i ) Having set bit set at same position as the only set bit in set_bit ( ii ) Having 0 bit at same position as the only set bit in set_bit ; XOR of two different sets are our required numbers . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printOdds ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res = res ^ arr [ i ] ; int set_bit = res & ( ~ ( res - 1 ) ) ; int x = 0 , y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & set_bit ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } cout << x << " ▁ " << y ; } int main ( ) { int arr [ ] = { 2 , 3 , 3 , 4 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printOdds ( arr , n ) ; return 0 ; }
Maximum subset with bitwise OR equal to k | CPP Program to find the maximum subset with bitwise OR equal to k ; function to find the maximum subset with bitwise OR equal to k ; If the bitwise OR of k and element is equal to k , then include that element in the subset ; Store the bitwise OR of elements in v ; If ans is not equal to k , subset doesn 't exist ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void subsetBitwiseORk ( int arr [ ] , int n , int k ) { vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] k ) == k ) v . push_back ( arr [ i ] ) ; } int ans = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) ans |= v [ i ] ; if ( ans != k ) { cout << " Subset ▁ does ▁ not ▁ exist " << endl ; return ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) cout << v [ i ] << ' ▁ ' ; } int main ( ) { int k = 3 ; int arr [ ] = { 1 , 4 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; subsetBitwiseORk ( arr , n , k ) ; return 0 ; }
Number whose XOR sum with given array is a given number k | CPP Program to find the number whose XOR sum with given array is equal to a given number k ; This function returns the number to be inserted in the given array ; initialise the answer with k ; ans ^= A [ i ] ; XOR of all elements in the array ; Driver Code to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findEletobeInserted ( int A [ ] , int n , int k ) { int ans = k ; for ( int i = 0 ; i < n ; i ++ ) return ans ; } int main ( ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int k = 10 ; cout << findEletobeInserted ( A , n , k ) << " ▁ has ▁ to ▁ be ▁ inserted " " ▁ in ▁ the ▁ given ▁ array ▁ to ▁ make ▁ xor ▁ sum ▁ of ▁ " << k << endl ; return 0 ; }
Left Shift and Right Shift Operators in C / C ++ | C ++ Program to demonstrate use of right shift operator ; a = 5 ( 00000101 ) , b = 9 ( 00001001 ) ; The result is 00000010 ; The result is 00000100
#include <iostream> NEW_LINE using namespace std ; int main ( ) { unsigned char a = 5 , b = 9 ; cout << " a > > 1 ▁ = ▁ " << ( a >> 1 ) << endl ; cout << " b > > 1 ▁ = ▁ " << ( b >> 1 ) << endl ; return 0 ; }