codeparrot/xlcost-text-to-code huggingface dataset:
Efficient Program to Compute Sum of Series 1 / 1 ! + 1 / 2 ! + 1 / 3 ! + 1 / 4 ! + . . + 1 / n ! | A simple C ++ program to compute sum of series 1 / 1 ! + 1 / 2 ! + . . + 1 / n ! ; An Efficient Function to return value of 1 / 1 ! + 1 / 2 ! + . . + 1 / n ! ; Update factorial ; Update series sum ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; double sum ( int n ) { double sum = 0 ; int fact = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { fact *= i ; sum += 1.0 / fact ; } return sum ; } int main ( ) { int n = 5 ; cout << sum ( n ) ; return 0 ; }
Print first k digits of 1 / n where n is a positive integer | CPP code to Print first k digits of 1 / n where n is a positive integer ; Function to print first k digits after dot in value of 1 / n . n is assumed to be a positive integer . ; Initialize remainder ; Run a loop k times to print k digits ; The next digit can always be obtained as doing ( 10 * rem ) / 10 ; Update remainder ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; void print ( int n , int k ) { int rem = 1 ; for ( int i = 0 ; i < k ; i ++ ) { cout << ( 10 * rem ) / n ; rem = ( 10 * rem ) % n ; } } int main ( ) { int n = 7 , k = 3 ; print ( n , k ) ; cout << endl ; n = 21 , k = 4 ; print ( n , k ) ; return 0 ; }
Program to find sum of series 1 + 1 / 2 + 1 / 3 + 1 / 4 + . . + 1 / n | C ++ program to find sum of series ; Function to return sum of 1 / 1 + 1 / 2 + 1 / 3 + . . + 1 / n ; Driver code
#include <iostream> NEW_LINE using namespace std ; class gfg { public : double sum ( int n ) { double i , s = 0.0 ; for ( i = 1 ; i <= n ; i ++ ) s = s + 1 / i ; return s ; } } ; int main ( ) { gfg g ; int n = 5 ; cout << " Sum ▁ is ▁ " << g . sum ( n ) ; return 0 ; }
Program to find GCD or HCF of two numbers | C ++ program to find GCD of two numbers ; Recursive function to return gcd of a and b ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int main ( ) { int a = 98 , b = 56 ; cout << " GCD ▁ of ▁ " << a << " ▁ and ▁ " << b << " ▁ is ▁ " << gcd ( a , b ) ; return 0 ; }
Rearrange an array so that arr [ i ] becomes arr [ arr [ i ] ] with O ( 1 ) extra space |  ; The function to rearrange an array in - place so that arr [ i ] becomes arr [ arr [ i ] ] . ; First step : Increase all values by ( arr [ arr [ i ] ] % n ) * n ; Second Step : Divide all values by n ; A utility function to print an array of size n ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; void rearrange ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] += ( arr [ arr [ i ] ] % n ) * n ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] /= n ; } void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int arr [ ] = { 3 , 2 , 0 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Given ▁ array ▁ is ▁ STRNEWLINE " ; printArr ( arr , n ) ; rearrange ( arr , n ) ; cout << " Modified ▁ array ▁ is ▁ STRNEWLINE " ; printArr ( arr , n ) ; return 0 ; }
Print all sequences of given length | C ++ program of above approach ; A utility function that prints a given arr [ ] of length size ; The core function that recursively generates and prints all sequences of length k ; A function that uses printSequencesRecur ( ) to prints all sequences from 1 , 1 , . .1 to n , n , . . n ; Driver Program to test above functions
#include <iostream> NEW_LINE using namespace std ; void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) cout << " ▁ " << arr [ i ] ; cout << " STRNEWLINE " ; return ; } void printSequencesRecur ( int arr [ ] , int n , int k , int index ) { int i ; if ( k == 0 ) { printArray ( arr , index ) ; } if ( k > 0 ) { for ( i = 1 ; i <= n ; ++ i ) { arr [ index ] = i ; printSequencesRecur ( arr , n , k - 1 , index + 1 ) ; } } } void printSequences ( int n , int k ) { int * arr = new int [ k ] ; printSequencesRecur ( arr , n , k , 0 ) ; return ; } int main ( ) { int n = 3 ; int k = 2 ; printSequences ( n , k ) ; return 0 ; }
Check if a number is multiple of 5 without using / and % operators |  ; assumes that n is a positive integer ; Driver Code
#include <iostream> NEW_LINE using namespace std ; bool isMultipleof5 ( int n ) { while ( n > 0 ) n = n - 5 ; if ( n == 0 ) return true ; return false ; } int main ( ) { int n = 19 ; if ( isMultipleof5 ( n ) == true ) cout << n << " ▁ is ▁ multiple ▁ of ▁ 5" ; else cout << n << " ▁ is ▁ not ▁ a ▁ multiple ▁ of ▁ 5" ; return 0 ; }
Count pairs from an array having sum of twice of their AND and XOR equal to K | C ++ program for the above approach ; Function to count number of pairs satisfying the given conditions ; Stores the frequency of array elements ; Stores the total number of pairs ; Traverse the array ; Add it to cnt ; Update frequency of current array element ; Print the count ; Driver Code ; Given array ; Size of the array ; Given value of K
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPairs ( int arr [ ] , int N , int K ) { unordered_map < int , int > mp ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cnt += mp [ K - arr [ i ] ] ; mp [ arr [ i ] ] ++ ; } cout << cnt ; } int main ( ) { int arr [ ] = { 1 , 5 , 4 , 8 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 9 ; countPairs ( arr , N , K ) ; return 0 ; }
Queries to count array elements from a given range having a single set bit | C ++ program for the above approach ; Function to check whether only one bit is set or not ; Function to perform Range - query ; Function to count array elements with a single set bit for each range in a query ; Initialize array for Prefix sum ; Driver Code ; Given array ; Size of the array ; Given queries ; Size of queries array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int check ( int x ) { if ( ( ( x ) & ( x - 1 ) ) == 0 ) return 1 ; return 0 ; } int query ( int l , int r , int pre [ ] ) { if ( l == 0 ) return pre [ r ] ; else return pre [ r ] - pre [ l - 1 ] ; } void countInRange ( int arr [ ] , int N , vector < pair < int , int > > queries , int Q ) { int pre [ N ] = { 0 } ; pre [ 0 ] = check ( arr [ 0 ] ) ; for ( int i = 1 ; i < N ; i ++ ) { pre [ i ] = pre [ i - 1 ] + check ( arr [ i ] ) ; } int c = 0 ; while ( Q -- ) { int l = queries . first ; int r = queries . second ; c ++ ; cout << query ( l , r , pre ) << ' ▁ ' ; } } int main ( ) { int arr [ ] = { 12 , 11 , 16 , 8 , 2 , 5 , 1 , 3 , 256 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; vector < pair < int , int > > queries = { { 0 , 9 } , { 4 , 9 } } ; int Q = queries . size ( ) ; countInRange ( arr , N , queries , Q ) ; return 0 ; }
Bitwise operations on Subarrays of size K | C ++ program for minimum values of each bitwise AND operation on elements of subarray of size K ; Function to convert bit array to decimal number ; Function to find minimum values of each bitwise AND operation on element of subarray of size K ; Maintain an integer array bit [ ] of size 32 all initialized to 0 ; Create a sliding window of size k ; Function call ; Perform operation to removed element ; Perform operation to add element ; Taking minimum value ; Return the result ; Driver Code ; Given array arr [ ] ; Given subarray size K ; Function Call
#include <iostream> NEW_LINE using namespace std ; int build_num ( int bit [ ] , int k ) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] == k ) ans += ( 1 << i ) ; return ans ; } int minimumAND ( int arr [ ] , int n , int k ) { int bit [ 32 ] = { 0 } ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( arr [ i ] & ( 1 << j ) ) bit [ j ] ++ ; } } int min_and = build_num ( bit , k ) ; for ( int i = k ; i < n ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( arr [ i - k ] & ( 1 << j ) ) bit [ j ] -- ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( arr [ i ] & ( 1 << j ) ) bit [ j ] ++ ; } min_and = min ( build_num ( bit , k ) , min_and ) ; } return min_and ; } int main ( ) { int arr [ ] = { 2 , 5 , 3 , 6 , 11 , 13 } ; int k = 3 ; int n = sizeof arr / sizeof arr [ 0 ] ; cout << minimumAND ( arr , n , k ) ; return 0 ; }
Equal Sum and XOR of three Numbers | C ++ implementation of the above approach ; Function to calculate power of 3 ; Function to return the count of the unset bit ( zeros ) ; Check the bit is 0 or not ; Right shifting ( dividing by 2 ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef unsigned long long int ull ; ull calculate ( int bit_cnt ) { ull res = 1 ; while ( bit_cnt -- ) { res = res * 3 ; } return res ; } int unset_bit_count ( ull n ) { int count = 0 ; while ( n ) { if ( ( n & 1 ) == 0 ) count ++ ; n = n >> 1 ; } return count ; } int main ( ) { ull n ; n = 2 ; int count = unset_bit_count ( n ) ; ull ans = calculate ( count ) ; cout << ans << endl ; return 0 ; }
Count pairs of elements such that number of set bits in their AND is B [ i ] | C ++ implementation of the approach ; Function to return the count of pairs which satisfy the given condition ; Check if the count of set bits in the AND value is B [ j ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int A [ ] , int B [ ] , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i ; j < n ; j ++ ) if ( __builtin_popcount ( A [ i ] & A [ j ] ) == B [ j ] ) { cnt ++ ; } return cnt ; } int main ( ) { int A [ ] = { 2 , 3 , 1 , 4 , 5 } ; int B [ ] = { 2 , 2 , 1 , 4 , 2 } ; int size = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << solve ( A , B , size ) ; return 0 ; }
Total pairs in an array such that the bitwise AND , bitwise OR and bitwise XOR of LSB is 1 | C ++ implementation of the approach ; Function to find the count of required pairs ; To store the count of elements which give remainder 0 i . e . even values ; To store the count of elements which give remainder 1 i . e . odd values ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void CalculatePairs ( int a [ ] , int n ) { int cnt_zero = 0 ; int cnt_one = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 0 ) cnt_zero += 1 ; else cnt_one += 1 ; } long int total_XOR_pairs = cnt_zero * cnt_one ; long int total_AND_pairs = ( cnt_one ) * ( cnt_one - 1 ) / 2 ; long int total_OR_pairs = cnt_zero * cnt_one + ( cnt_one ) * ( cnt_one - 1 ) / 2 ; cout << " cntXOR ▁ = ▁ " << total_XOR_pairs << endl ; cout << " cntAND ▁ = ▁ " << total_AND_pairs << endl ; cout << " cntOR ▁ = ▁ " << total_OR_pairs << endl ; } int main ( ) { int a [ ] = { 1 , 3 , 4 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; CalculatePairs ( a , n ) ; return 0 ; }
Assign other value to a variable from two possible values | CPP program to change value of x according to its current value . ; Function to alternate the values ; Main function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void alternate ( int & a , int & b , int & x ) { x = a + b - x ; } int main ( ) { int a = -10 ; int b = 15 ; int x = a ; cout << " x ▁ is ▁ : ▁ " << x ; alternate ( a , b , x ) ; cout << " After change " cout << " STRNEWLINE x ▁ is ▁ : ▁ " << x ; }
Highest power of two that divides a given number | CPP program to find highest power of 2 that divides n . ; Driver code
#include <iostream> NEW_LINE using namespace std ; int highestPowerOf2 ( int n ) { return ( n & ( ~ ( n - 1 ) ) ) ; } int main ( ) { int n = 48 ; cout << highestPowerOf2 ( n ) ; return 0 ; }
Check whether bitwise AND of a number with any subset of an array is zero or not | C ++ program to check whether bitwise AND of a number with any subset of an array is zero or not ; Function to check whether bitwise AND of a number with any subset of an array is zero or not ; variable to store the AND of all the elements ; find the AND of all the elements of the array ; if the AND of all the array elements and N is equal to zero ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isSubsetAndZero ( int array [ ] , int length , int N ) { int arrAnd = array [ 0 ] ; for ( int i = 1 ; i < length ; i ++ ) { arrAnd = arrAnd & array [ i ] ; } if ( ( arrAnd & N ) == 0 ) cout << " YES " << endl ; else cout << " NO " << endl ; } int main ( ) { int array [ ] = { 1 , 2 , 4 } ; int length = sizeof ( array ) / sizeof ( int ) ; int N = 3 ; isSubsetAndZero ( array , length , N ) ; }
Finding the Parity of a number Efficiently | Program to find the parity of a given number ; Function to find the parity ; Rightmost bit of y holds the parity value if ( y & 1 ) is 1 then parity is odd else even ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findParity ( int x ) { int y = x ^ ( x >> 1 ) ; y = y ^ ( y >> 2 ) ; y = y ^ ( y >> 4 ) ; y = y ^ ( y >> 8 ) ; y = y ^ ( y >> 16 ) ; if ( y & 1 ) return 1 ; return 0 ; } int main ( ) { ( findParity ( 9 ) == 0 ) ? cout << " Even ▁ Parity STRNEWLINE " : cout << " Odd ▁ Parity STRNEWLINE " ; ( findParity ( 13 ) == 0 ) ? cout << " Even ▁ Parity STRNEWLINE " : cout << " Odd ▁ Parity STRNEWLINE " ; return 0 ; }
Check if bits in range L to R of two numbers are complement of each other or not | C ++ implementation to check whether all the bits in the given range of two numbers are complement of each other ; function to check whether all the bits are set in the given range or not ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; new number which will only have one or more set bits in the range l to r and nowhere else ; if both are equal , then all bits are set in the given range ; else all bits are not set ; function to check whether all the bits in the given range of two numbers are complement of each other ; Driver Code
#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 ( num == new_num ) return true ; return false ; } bool bitsAreComplement ( unsigned int a , unsigned int b , unsigned int l , unsigned int r ) { unsigned int xor_value = a ^ b ; return allBitsSetInTheGivenRange ( xor_value , l , r ) ; } int main ( ) { unsigned int a = 10 , b = 5 ; unsigned int l = 1 , r = 3 ; if ( bitsAreComplement ( a , b , l , r ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Sum of the series 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + ... . . + 2 ^ n | C ++ program to find sum ; function to calculate sum of series ; initialize sum as 0 ; loop to calculate sum of series ; calculate 2 ^ i and add it to sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateSum ( int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( 1 << i ) ; } return sum ; } int main ( ) { int n = 10 ; cout << " Sum ▁ of ▁ series ▁ of ▁ power ▁ of ▁ 2 ▁ is ▁ : ▁ " << calculateSum ( n ) ; }
Print all the combinations of N elements by changing sign such that their sum is divisible by M |  ; Function to print all the combinations ; Iterate for all combinations ; Initially 100 in binary if n is 3 as 1 << ( 3 - 1 ) = 100 in binary ; Iterate in the array and assign signs to the array elements ; If the j - th bit from left is set take ' + ' sign ; Right shift to check if jth bit is set or not ; re - initialize ; Iterate in the array elements ; If the jth from left is set ; right shift ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printCombinations ( int a [ ] , int n , int m ) { for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int sum = 0 ; int num = 1 << ( n - 1 ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( i & num ) sum += a [ j ] ; else sum += ( -1 * a [ j ] ) ; num = num >> 1 ; } if ( sum % m == 0 ) { num = 1 << ( n - 1 ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & num ) ) cout << " + " << a [ j ] << " ▁ " ; else cout << " - " << a [ j ] << " ▁ " ; num = num >> 1 ; } cout << endl ; } } } int main ( ) { int a [ ] = { 3 , 5 , 6 , 8 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int m = 5 ; printCombinations ( a , n , m ) ; return 0 ; }
Same Number Of Set Bits As N | CPP program to find numbers less than N that have same Number Of Set Bits As N ; function ; __builtin_popcount function that count set bits in n ; Iterate from n - 1 to 1 ; check if the number of set bits equals to temp increment count ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int smallerNumsWithSameSetBits ( int n ) { int temp = __builtin_popcount ( n ) ; int count = 0 ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( temp == __builtin_popcount ( i ) ) count ++ ; } return count ; } int main ( ) { int n = 4 ; cout << smallerNumsWithSameSetBits ( n ) ; return 0 ; }
Multiply any Number with 4 using Bitwise Operator | C ++ program to multiply a number with 4 using Bitwise Operator ; function the return multiply a number with 4 using bitwise operator ; returning a number with multiply with 4 using2 bit shifring right ; derive function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int multiplyWith4 ( int n ) { return ( n << 2 ) ; } int main ( ) { int n = 4 ; cout << multiplyWith4 ( n ) << endl ; return 0 ; }
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 . ; array to store the tree ; array to store the level of every parent ; function to construct the tree ; level of child is always 0 ; recursive call ; increase the level of every parent , which is level of child + 1 ; if the parent is at odd level , then do a bitwise OR ; if the parent is at even level , then do a bitwise XOR ; function that updates the tree ; if it is a leaf and the leaf which is to be updated ; out of range ; not a leaf then recurse ; recursive call ; check if the parent is at odd or even level and perform OR or XOR according to that ; function that assigns value to a [ index ] and calls update function to update the tree ; Driver Code ; builds the tree ; 1 st query ; 2 nd query
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  1000 NEW_LINE int tree [ N ] ; int level [ N ] ; void constructTree ( int low , int high , int pos , int a [ ] ) { if ( low == high ) { level [ pos ] = 0 ; tree [ pos ] = a [ high ] ; return ; } int mid = ( low + high ) / 2 ; constructTree ( low , mid , 2 * pos + 1 , a ) ; constructTree ( mid + 1 , high , 2 * pos + 2 , a ) ; level [ pos ] = level [ 2 * pos + 1 ] + 1 ; if ( level [ pos ] & 1 ) tree [ pos ] = tree [ 2 * pos + 1 ] | tree [ 2 * pos + 2 ] ; else tree [ pos ] = tree [ 2 * pos + 1 ] ^ tree [ 2 * pos + 2 ] ; } void update ( int low , int high , int pos , int index , int a [ ] ) { if ( low == high and low == index ) { tree [ pos ] = a [ low ] ; return ; } if ( index < low index > high ) return ; if ( low != high ) { int mid = ( low + high ) / 2 ; update ( low , mid , 2 * pos + 1 , index , a ) ; update ( mid + 1 , high , 2 * pos + 2 , index , a ) ; if ( level [ pos ] & 1 ) tree [ pos ] = tree [ 2 * pos + 1 ] | tree [ 2 * pos + 2 ] ; else tree [ pos ] = tree [ 2 * pos + 1 ] ^ tree [ 2 * pos + 2 ] ; } } void updateValue ( int index , int value , int a [ ] , int n ) { a [ index ] = value ; update ( 0 , n - 1 , 0 , index , a ) ; } int main ( ) { int a [ ] = { 1 , 4 , 5 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; constructTree ( 0 , n - 1 , 0 , a ) ; int index = 0 ; int value = 2 ; updateValue ( index , value , a , n ) ; cout << tree [ 0 ] << endl ; index = 3 ; value = 5 ; updateValue ( index , value , a , n ) ; cout << tree [ 0 ] << endl ; return 0 ; }
Set all even bits of a number | Simple CPP program to set all even bits of a number ; Sets even bits of n and returns modified number . ; Generate 101010. . .10 number and store in res . ; if bit is even then generate number and or with res ; return OR number ;
#include <iostream> NEW_LINE using namespace std ; int evenbitsetnumber ( int n ) { int count = 0 , res = 0 ; for ( int temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 1 ) res |= ( 1 << count ) ; count ++ ; } return ( n res ) ; } / * Driver code * int main ( ) { int n = 10 ; cout << evenbitsetnumber ( n ) ; return 0 ; }
Set all even bits of a number | Efficient CPP program to set all even bits of a number ; return msb set number ; set all bits ; return msb increment n by 1 and shift by 1 ; return even seted number ; get msb here ; generate even bits like 101010. . ; if bits is odd then shift by 1 ; return even set bits number ; set all even bits here ; take or with even set bits number ; Driver code
#include <iostream> NEW_LINE using namespace std ; int getmsb ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( n + 1 ) >> 1 ; } int getevenbits ( int n ) { n = getmsb ( n ) ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; if ( n & 1 ) n = n >> 1 ; return n ; } int setallevenbits ( int n ) { return n | getevenbits ( n ) ; } int main ( ) { int n = 10 ; cout << setallevenbits ( n ) ; return 0 ; }
Set all odd bits of a number | CPP code Set all odd bits of a number ; set all odd bit ; res for store 010101. . number ; generate number form of 010101. . ... till temp size ; if bit is odd , then generate number and or with res ; Driver code
#include <iostream> NEW_LINE using namespace std ; int oddbitsetnumber ( int n ) { int count = 0 ; int res = 0 ; for ( int temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 0 ) res |= ( 1 << count ) ; count ++ ; } return ( n res ) ; } int main ( ) { int n = 10 ; cout << oddbitsetnumber ( n ) ; return 0 ; }
Set all odd bits of a number | Efficient CPP program to set all odd bits number ; return MSB set number ; set all bits including MSB . ; return MSB ; Returns a number of same size ( MSB atsame position ) as n and all odd bits set . ; generate odd bits like 010101. . ; if bits is even then shift by 1 ; return odd set bits number ; set all odd bits here ; take OR with odd set bits number ; Driver code
#include <iostream> NEW_LINE using namespace std ; int getmsb ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( n + 1 ) >> 1 ; } int getevenbits ( int n ) { n = getmsb ( n ) ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; if ( ( n & 1 ) == 0 ) n = n >> 1 ; return n ; } int setalloddbits ( int n ) { return n | getevenbits ( n ) ; } int main ( ) { int n = 10 ; cout << setalloddbits ( n ) ; return 0 ; }
Print numbers in the range 1 to n having bits in alternate pattern | C ++ implementation to print numbers in the range 1 to n having bits in alternate pattern ; function to print numbers in the range 1 to n having bits in alternate pattern ; first number having bits in alternate pattern ; display ; loop until n < curr_num ; generate next number having alternate bit pattern ; if true then break ; display ; generate next number having alternate bit pattern ; if true then break ; display ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printNumHavingAltBitPatrn ( int n ) { int curr_num = 1 ; cout << curr_num << " ▁ " ; while ( 1 ) { curr_num <<= 1 ; if ( n < curr_num ) break ; cout << curr_num << " ▁ " ; curr_num = ( ( curr_num ) << 1 ) ^ 1 ; if ( n < curr_num ) break ; cout << curr_num << " ▁ " ; } } int main ( ) { int n = 50 ; printNumHavingAltBitPatrn ( n ) ; return 0 ; }
Smallest perfect power of 2 greater than n ( without using arithmetic operators ) | C ++ implementation of smallest perfect power of 2 greater than n ; Function to find smallest perfect power of 2 greater than n ; To store perfect power of 2 ; bitwise left shift by 1 ; bitwise right shift by 1 ; Required perfect power of 2 ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int perfectPowerOf2 ( unsigned int n ) { unsigned int per_pow = 1 ; while ( n > 0 ) { per_pow = per_pow << 1 ; n = n >> 1 ; } return per_pow ; } int main ( ) { unsigned int n = 128 ; cout << " Perfect ▁ power ▁ of ▁ 2 ▁ greater ▁ than ▁ " << n << " : ▁ " << perfectPowerOf2 ( n ) ; return 0 ; }