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 Java 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
import java . io . * ; class GFG { static 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 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( sum ( n ) ) ; } }
Print first k digits of 1 / n where n is a positive integer | Java 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
import java . io . * ; class GFG { static void print ( int n , int k ) { int rem = 1 ; for ( int i = 0 ; i < k ; i ++ ) { System . out . print ( ( 10 * rem ) / n ) ; rem = ( 10 * rem ) % n ; } } public static void main ( String [ ] args ) { int n = 7 , k = 3 ; print ( n , k ) ; System . out . println ( ) ; n = 21 ; k = 4 ; print ( n , k ) ; } }
Program to find sum of series 1 + 1 / 2 + 1 / 3 + 1 / 4 + . . + 1 / n | Java Program to find sum of series ; Function to return sum of 1 / 1 + 1 / 2 + 1 / 3 + . . + 1 / n ; Driven Program
import java . io . * ; class GFG { static double sum ( int n ) { double i , s = 0.0 ; for ( i = 1 ; i <= n ; i ++ ) s = s + 1 / i ; return s ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . printf ( " Sum ▁ is ▁ % f " , sum ( n ) ) ; } }
Program to find GCD or HCF of two numbers | Java program to find GCD of two numbers ; Recursive function to return gcd of a and b ; Driver method
class Test { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int a = 98 , b = 56 ; System . out . println ( " GCD ▁ of ▁ " + a + " ▁ and ▁ " + b + " ▁ is ▁ " + gcd ( a , b ) ) ; } }
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
class Rearrange { 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 ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { Rearrange rearrange = new Rearrange ( ) ; int arr [ ] = { 3 , 2 , 0 , 1 } ; int n = arr . length ; System . out . println ( " Given ▁ Array ▁ is ▁ : " ) ; rearrange . printArr ( arr , n ) ; rearrange . rearrange ( arr , n ) ; System . out . println ( " Modified ▁ Array ▁ is ▁ : " ) ; rearrange . printArr ( arr , n ) ; } }
Print all sequences of given length | Java 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
class GfG { static void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; return ; } static 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 ) ; } } } static void printSequences ( int n , int k ) { int arr [ ] = new int [ k ] ; printSequencesRecur ( arr , n , k , 0 ) ; return ; } public static void main ( String [ ] args ) { int n = 3 ; int k = 2 ; printSequences ( n , k ) ; } }
Check if a number is multiple of 5 without using / and % operators | Java code to check if a number is multiple of 5 without using ' / ' and ' % ' operators ; assumes that n is a positive integer ; Driver Code
class GFG { static boolean isMultipleof5 ( int n ) { while ( n > 0 ) n = n - 5 ; if ( n == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 19 ; if ( isMultipleof5 ( n ) == true ) System . out . printf ( "%d is multiple of 5NEW_LINE", n); else System . out . printf ( "%d is not a multiple of 5NEW_LINE", n); } }
Count pairs from an array having sum of twice of their AND and XOR equal to K | Java 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
import java . io . * ; import java . util . * ; class GFG { static void countPairs ( int arr [ ] , int N , int K ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . get ( K - arr [ i ] ) != null ) cnt += mp . get ( K - arr [ i ] ) ; mp . put ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ; } System . out . println ( cnt ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 4 , 8 , 7 } ; int N = arr . length ; int K = 9 ; countPairs ( arr , N , K ) ; } }
Queries to count array elements from a given range having a single set bit | JAVA 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 ; A Pair class for handling queries in JAVA As , there is no in - built function of Tuple ; Driver code ; Given array ; Size of the array ; Given queries ; Size of queries array
import java . util . * ; import java . io . * ; import java . math . * ; public class GFG { static int check ( int x ) { if ( ( ( x ) & ( x - 1 ) ) == 0 ) return 1 ; return 0 ; } static int query ( int l , int r , int [ ] pre ) { if ( l == 0 ) return pre [ r ] ; else return pre [ r ] - pre [ l - 1 ] ; } static void countInRange ( int [ ] arr , int N , ArrayList < Pair > queries , int Q ) { int [ ] pre = new int [ N ] ; pre [ 0 ] = check ( arr [ 0 ] ) ; for ( int i = 1 ; i < N ; i ++ ) { pre [ i ] = pre [ i - 1 ] + check ( arr [ i ] ) ; } int c = 0 ; int q = 0 ; while ( q < Q ) { int l = queries . get ( q ) . item1 ; int r = queries . get ( q ) . item2 ; c ++ ; q ++ ; System . out . print ( query ( l , r , pre ) + " ▁ " ) ; } } static class Pair { int item1 , item2 ; Pair ( int item1 , int item2 ) { this . item1 = item1 ; this . item2 = item2 ; } } public static void main ( String args [ ] ) { int [ ] arr = { 12 , 11 , 16 , 8 , 2 , 5 , 1 , 3 , 256 , 1 } ; int N = arr . length ; ArrayList < Pair > queries = new ArrayList < Pair > ( ) ; queries . add ( new Pair ( 4 , 9 ) ) ; queries . add ( new Pair ( 0 , 9 ) ) ; int Q = queries . size ( ) ; countInRange ( arr , N , queries , Q ) ; } }
Bitwise operations on Subarrays of size K | Java 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
class GFG { static 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 ; } static int minimumAND ( int arr [ ] , int n , int k ) { int bit [ ] = new int [ 32 ] ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) 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 ) ) > 0 ) bit [ j ] -- ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } min_and = Math . min ( build_num ( bit , k ) , min_and ) ; } return min_and ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 3 , 6 , 11 , 13 } ; int k = 3 ; int n = arr . length ; System . out . print ( minimumAND ( arr , n , k ) ) ; } }
Equal Sum and XOR of three Numbers | Java implementation of the 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
import java . util . * ; class GFG { static long calculate ( int bit_cnt ) { long res = 1 ; while ( bit_cnt -- > 0 ) { res = res * 3 ; } return res ; } static int unset_bit_count ( long n ) { int count = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) count ++ ; n = n >> 1 ; } return count ; } public static void main ( String [ ] args ) { long n ; n = 2 ; int count = unset_bit_count ( n ) ; long ans = calculate ( count ) ; System . out . println ( ans ) ; } }
Count pairs of elements such that number of set bits in their AND is B [ i ] | Java implementation of the approach ; Function to return the count of pairs which satisfy the given condition ; for ( int j = i ; j < n ; j ++ ) Check if the count of set bits in the AND value is B [ j ] ; Driver code
public class GFG { static int solve ( int A [ ] , int B [ ] , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { { if ( Integer . bitCount ( A [ i ] & A [ j ] ) == B [ j ] ) { cnt ++ ; } } } return cnt ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 3 , 1 , 4 , 5 } ; int B [ ] = { 2 , 2 , 1 , 4 , 2 } ; int size = A . length ; System . out . println ( solve ( A , B , size ) ) ; } }
Total pairs in an array such that the bitwise AND , bitwise OR and bitwise XOR of LSB is 1 | Java 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
class GFG { static 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 ; } int total_XOR_pairs = cnt_zero * cnt_one ; int total_AND_pairs = ( cnt_one ) * ( cnt_one - 1 ) / 2 ; int total_OR_pairs = cnt_zero * cnt_one + ( cnt_one ) * ( cnt_one - 1 ) / 2 ; System . out . println ( " cntXOR ▁ = ▁ " + total_XOR_pairs ) ; System . out . println ( " cntAND ▁ = ▁ " + total_AND_pairs ) ; System . out . println ( " cntOR ▁ = ▁ " + total_OR_pairs ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 4 , 2 } ; int n = a . length ; CalculatePairs ( a , n ) ; } }
Assign other value to a variable from two possible values | Java program to change value of x according to its current value . ; Function to alternate the values ; Main function
import java . util . * ; class solution { static void alternate ( int a , int b , int x ) { x = a + b - x ; System . out . println ( " After ▁ change " + " " + " x is : "+x); } public static void main ( String args [ ] ) { int a = - 10 ; int b = 15 ; int x = a ; System . out . println ( " x ▁ is ▁ : ▁ " + x ) ; alternate ( a , b , x ) ; } }
Highest power of two that divides a given number | Java program to find highest power of 2 that divides n . ; Driver code
class GFG { static int highestPowerOf2 ( int n ) { return ( n & ( ~ ( n - 1 ) ) ) ; } public static void main ( String [ ] args ) { int n = 48 ; System . out . println ( highestPowerOf2 ( n ) ) ; } }
Check whether bitwise AND of a number with any subset of an array is zero or not | Java 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
import java . io . * ; public class GFG { static 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 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int array [ ] = { 1 , 2 , 4 } ; int length = array . length ; 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
import java . io . * ; class GFG { static boolean 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 ) > 0 ) return true ; return false ; } public static void main ( String [ ] args ) { if ( ( findParity ( 9 ) == false ) ) System . out . println ( " Even ▁ Parity " ) ; else System . out . println ( " Odd ▁ Parity " ) ; if ( findParity ( 13 ) == false ) System . out . println ( " Even ▁ Parity " ) ; else System . out . println ( " Odd ▁ Parity " ) ; } }
Check if bits in range L to R of two numbers are complement of each other or not | Java 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
class GFG { static boolean 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 true ; return false ; } static boolean bitsAreComplement ( int a , int b , int l , int r ) { int xor_value = a ^ b ; return allBitsSetInTheGivenRange ( xor_value , l , r ) ; } public static void main ( String [ ] args ) { int a = 10 , b = 5 ; int l = 1 , r = 3 ; if ( bitsAreComplement ( a , b , l , r ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Sum of the series 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + ... . . + 2 ^ n | Java 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 ; Main function
class GFG { static int calculate sum ( int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( 1 << i ) ; } return sum ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( " Sum ▁ of ▁ the ▁ series ▁ : ▁ " + 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
import java . io . * ; class GFG { static 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 ) > 0 ) 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 ) > 0 ) System . out . print ( " + " + a [ j ] + " ▁ " ) ; else System . out . print ( " - " + a [ j ] + " ▁ " ) ; num = num >> 1 ; } System . out . println ( ) ; } } } public static void main ( String args [ ] ) { int a [ ] = { 3 , 5 , 6 , 8 } ; int n = a . length ; int m = 5 ; printCombinations ( a , n , m ) ; } }
Same Number Of Set Bits As N | Java 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
class GFG { static int __builtin_popcount ( int n ) { int d , t = 0 ; while ( n > 0 ) { d = n % 2 ; n = n / 2 ; if ( d == 1 ) t ++ ; } return t ; } static 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 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( smallerNumsWithSameSetBits ( n ) ) ; } }
Multiply any Number with 4 using Bitwise Operator | Java 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 using 2 bit shifting right ; Driver Code
class GFG { static int multiplyWith4 ( int n ) { return ( n << 2 ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( multiplyWith4 ( n ) ) ; } }
Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs | java 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
import java . io . * ; public class GFG { static int N = 1000 ; static int [ ] tree = new int [ N ] ; static int [ ] level = new int [ N ] ; static 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 ) > 0 ) tree [ pos ] = tree [ 2 * pos + 1 ] | tree [ 2 * pos + 2 ] ; else tree [ pos ] = tree [ 2 * pos + 1 ] ^ tree [ 2 * pos + 2 ] ; } static void update ( int low , int high , int pos , int index , int [ ] a ) { if ( low == high && 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 ) > 0 ) tree [ pos ] = tree [ 2 * pos + 1 ] | tree [ 2 * pos + 2 ] ; else tree [ pos ] = tree [ 2 * pos + 1 ] ^ tree [ 2 * pos + 2 ] ; } } static void updateValue ( int index , int value , int [ ] a , int n ) { a [ index ] = value ; update ( 0 , n - 1 , 0 , index , a ) ; } static public void main ( String [ ] args ) { int [ ] a = { 1 , 4 , 5 , 6 } ; int n = a . length ; ; constructTree ( 0 , n - 1 , 0 , a ) ; int index = 0 ; int value = 2 ; updateValue ( index , value , a , n ) ; System . out . println ( tree [ 0 ] ) ; index = 3 ; value = 5 ; updateValue ( index , value , a , n ) ; System . out . println ( tree [ 0 ] ) ; } }
Set all even bits of a number | Simple Java 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 ; Driver code
class GFG { static 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 ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( evenbitsetnumber ( n ) ) ; } }
Set all even bits of a number | Efficient Java 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
import java . io . * ; class GFG { static int getmsb ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( n + 1 ) >> 1 ; } static int getevenbits ( int n ) { n = getmsb ( n ) ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; if ( ( n & 1 ) == 1 ) n = n >> 1 ; return n ; } static int setallevenbits ( int n ) { return n | getevenbits ( n ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( setallevenbits ( n ) ) ; } }
Set all odd bits of a number | Java code to 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
class GFG { static 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 ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( oddbitsetnumber ( n ) ) ; } }
Set all odd bits of a number | Efficient Java 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 at same 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
class GFG { static int getmsb ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( n + 1 ) >> 1 ; } static 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 ; } static int setalloddbits ( int n ) { return n | getevenbits ( n ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( setalloddbits ( n ) ) ; } }