codeparrot/xlcost-text-to-code huggingface dataset:
Count total set bits in all numbers from 1 to n | Java A O ( Logn ) complexity program to count set bits in all numbers from 1 to n ; Returns position of leftmost set bit . The rightmost position is considered as 0 ; Given the position of previous leftmost set bit in n ( or an upper bound onleftmost position ) returns the new position of leftmost set bit in n ; The main recursive function used by countSetBits ( ) Returns count of set bits present in all numbers from 1 to n ; Get the position of leftmost set bit in n . This will be used as an upper bound for next set bit function ; Use the position ; Base Case : if n is 0 , then set bit count is 0 ; get position of next leftmost set bit ; If n is of the form 2 ^ x - 1 , i . e . , if n is like 1 , 3 , 7 , 15 , 31 , . . etc , then we are done . Since positions are considered starting from 0 , 1 is added to m ; update n for next recursive call ; Driver code
import java . io . * ; class GFG { static int getLeftmostBit ( int n ) { int m = 0 ; while ( n > 1 ) { n = n >> 1 ; m ++ ; } return m ; } static int getNextLeftmostBit ( int n , int m ) { int temp = 1 << m ; while ( n < temp ) { temp = temp >> 1 ; m -- ; } return m ; } static int countSetBits ( int n ) { int m = getLeftmostBit ( n ) ; return countSetBits ( n , m ) ; } static int countSetBits ( int n , int m ) { if ( n == 0 ) return 0 ; m = getNextLeftmostBit ( n , m ) ; if ( n == ( ( int ) 1 << ( m + 1 ) ) - 1 ) return ( int ) ( m + 1 ) * ( 1 << m ) ; n = n - ( 1 << m ) ; return ( n + 1 ) + countSetBits ( n ) + m * ( 1 << ( m - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 17 ; System . out . println ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " + countSetBits ( n ) ) ; } }
Swap bits in a given number | Java Program to swap bits in a given number ; Move all bits of first set to rightmost side ; Move all bits of second set to rightmost side ; XOR the two sets ; Put the xor bits back to their original positions ; XOR the ' xor ' with the original number so that the two sets are swapped ; Driver program
class GFG { static int swapBits ( int x , int p1 , int p2 , int n ) { int set1 = ( x >> p1 ) & ( ( 1 << n ) - 1 ) ; int set2 = ( x >> p2 ) & ( ( 1 << n ) - 1 ) ; int xor = ( set1 ^ set2 ) ; xor = ( xor << p1 ) | ( xor << p2 ) ; int result = x ^ xor ; return result ; } public static void main ( String [ ] args ) { int res = swapBits ( 28 , 0 , 3 , 2 ) ; System . out . println ( " Result ▁ = ▁ " + res ) ; } }
Swap bits in a given number |  ; Setting bit at p1 position to 1 ; Setting bit at p2 position to 1 ; value1 and value2 will have 0 if num at the respective positions - p1 and p2 is 0. ; check if value1 and value2 are different i . e . at one position bit is set and other it is not ; if bit at p1 position is set ; unset bit at p1 position ; set bit at p2 position ; if bit at p2 position is set ; set bit at p2 position ; unset bit at p2 position ; return final result ; Driver code
class GFG { static int swapBits ( int num , int p1 , int p2 , int n ) { int shift1 , shift2 , value1 , value2 ; while ( n -- > 0 ) { shift1 = 1 << p1 ; shift2 = 1 << p2 ; value1 = ( ( num & shift1 ) ) ; value2 = ( ( num & shift2 ) ) ; if ( ( value1 == 0 && value2 != 0 ) || ( value2 == 0 && value1 != 0 ) ) { if ( value1 != 0 ) { num = num & ( ~ shift1 ) ; num = num | shift2 ; } else { num = num & ( ~ shift2 ) ; num = num | shift1 ; } } p1 ++ ; p2 ++ ; } return num ; } public static void main ( String [ ] args ) { int res = swapBits ( 28 , 0 , 3 , 2 ) ; System . out . println ( " Result ▁ = ▁ " + res ) ; } }
Smallest of three integers without comparison operators | Java program to find Smallest of three integers without comparison operators ; Driver code
class GFG { static int smallest ( int x , int y , int z ) { int c = 0 ; while ( x != 0 && y != 0 && z != 0 ) { x -- ; y -- ; z -- ; c ++ ; } return c ; } public static void main ( String [ ] args ) { int x = 12 , y = 15 , z = 5 ; System . out . printf ( " Minimum ▁ of ▁ 3" + " ▁ numbers ▁ is ▁ % d " , smallest ( x , y , z ) ) ; } }
Smallest of three integers without comparison operators | Java implementation of above approach ; Function to find minimum of x and y ; Function to find minimum of 3 numbers x , y and z ; Driver code
class GFG { static int CHAR_BIT = 8 ; static int min ( int x , int y ) { return y + ( ( x - y ) & ( ( x - y ) >> ( ( Integer . SIZE / 8 ) * CHAR_BIT - 1 ) ) ) ; } static int smallest ( int x , int y , int z ) { return Math . min ( x , Math . min ( y , z ) ) ; } public static void main ( String [ ] args ) { int x = 12 , y = 15 , z = 5 ; System . out . println ( " Minimum ▁ of ▁ 3 ▁ numbers ▁ is ▁ " + smallest ( x , y , z ) ) ; } }
Smallest of three integers without comparison operators | Java program of above approach ; Using division operator to find minimum of three numbers ; Same as " if ▁ ( y ▁ < ▁ x ) " ; Driver code
class GfG { static int smallest ( int x , int y , int z ) { if ( ( y / x ) != 1 ) return ( ( y / z ) != 1 ) ? y : z ; return ( ( x / z ) != 1 ) ? x : z ; } public static void main ( String [ ] args ) { int x = 78 , y = 88 , z = 68 ; System . out . printf ( " Minimum ▁ of ▁ 3 ▁ numbers " + " ▁ is ▁ % d " , smallest ( x , y , z ) ) ; } }
A Boolean Array Puzzle |  ; Driver code
import java . io . * ; class GFG { public static void changeToZero ( int a [ ] ) { a [ a [ 1 ] ] = a [ 1 - a [ 1 ] ] ; } public static void main ( String args [ ] ) { int [ ] arr ; arr = new int [ 2 ] ; arr [ 0 ] = 1 ; arr [ 1 ] = 0 ; changeToZero ( arr ) ; System . out . println ( " arr [ 0 ] = ▁ " + arr [ 0 ] ) ; System . out . println ( " arr [ 1 ] = ▁ " + arr [ 1 ] ) ; } }
Next higher number with same number of set bits | Java Implementation on above approach ; this function returns next higher number with same number of set bits as x . ; right most set bit ; reset the pattern and set next higher bit left part of x will be here ; nextHigherOneBit is now part [ D ] of the above explanation . isolate the pattern ; right adjust pattern ; correction factor ; rightOnesPattern is now part [ A ] of the above explanation . integrate new pattern ( Add [ D ] and [ A ] ) ; Driver code
class GFG { static int snoob ( int x ) { int rightOne , nextHigherOneBit , rightOnesPattern , next = 0 ; if ( x > 0 ) { rightOne = x & - x ; nextHigherOneBit = x + rightOne ; rightOnesPattern = x ^ nextHigherOneBit ; rightOnesPattern = ( rightOnesPattern ) / rightOne ; rightOnesPattern >>= 2 ; next = nextHigherOneBit | rightOnesPattern ; } return next ; } public static void main ( String [ ] args ) { int x = 156 ; System . out . println ( " Next ▁ higher ▁ number ▁ with ▁ same " + " number ▁ of ▁ set ▁ bits ▁ is ▁ " + snoob ( x ) ) ; } }
Add 1 to a given number | Java code to add add one to a given number ; Flip all the set bits until we find a 0 ; flip the rightmost 0 bit ; Driver program to test above functions
class GFG { static int addOne ( int x ) { int m = 1 ; while ( ( int ) ( x & m ) >= 1 ) { x = x ^ m ; m <<= 1 ; } x = x ^ m ; return x ; } public static void main ( String [ ] args ) { System . out . println ( addOne ( 13 ) ) ; } }
Add 1 to a given number | Java code to Add 1 to a given number ; Driver program
class GFG { static int addOne ( int x ) { return ( - ( ~ x ) ) ; } public static void main ( String [ ] args ) { System . out . printf ( " % d " , addOne ( 13 ) ) ; } }
Multiply a given Integer with 3.5 | Java Program to multiply a number with 3.5 ; Driver program to test above functions
class GFG { static int multiplyWith3Point5 ( int x ) { return ( x << 1 ) + x + ( x >> 1 ) ; } public static void main ( String [ ] args ) { int x = 2 ; System . out . println ( multiplyWith3Point5 ( x ) ) ; } }
Multiply a given Integer with 3.5 | Java program for above approach ; Function to multiple number with 3.5 ; The 3.5 is 7 / 2 , so multiply by 7 ( x * 7 ) then divide the result by 2 ( result / 2 ) x * 7 -> 7 is 0111 so by doing mutiply by 7 it means we do 2 shifting for the number but since we doing multiply we need to take care of carry one . ; Then divide by 2 r / 2 ; Driver Code
import java . io . * ; class GFG { static int multiplyWith3Point5 ( int x ) { int r = 0 ; int x1Shift = x << 1 ; int x2Shifts = x << 2 ; r = ( x ^ x1Shift ) ^ x2Shifts ; int c = ( x & x1Shift ) | ( x & x2Shifts ) | ( x1Shift & x2Shifts ) ; while ( c > 0 ) { c <<= 1 ; int t = r ; r ^= c ; c &= t ; } r = r >> 1 ; return r ; } public static void main ( String [ ] args ) { System . out . println ( multiplyWith3Point5 ( 5 ) ) ; } }
Turn off the rightmost set bit | Java program to unset the rightmost set bit of an integer . ; unsets the rightmost set bit of n and returns the result ; Driver code
class GFG { static int fun ( int n ) { return n & ( n - 1 ) ; } public static void main ( String arg [ ] ) { int n = 7 ; System . out . print ( " The ▁ number ▁ after ▁ unsetting ▁ " + " the ▁ rightmost ▁ set ▁ bit ▁ " + fun ( n ) ) ; } }
Find whether a given number is a power of 4 or not | Java code to check if given number is power of 4 or not ; Function to check if x is power of 4 ; Driver program
class GFG { static int isPowerOfFour ( int n ) { if ( n == 0 ) return 0 ; while ( n != 1 ) { if ( n % 4 != 0 ) return 0 ; n = n / 4 ; } return 1 ; } public static void main ( String [ ] args ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) == 1 ) System . out . println ( test_no + " ▁ is ▁ a ▁ power ▁ of ▁ 4" ) ; else System . out . println ( test_no + " is ▁ not ▁ a ▁ power ▁ of ▁ 4" ) ; } }
Find whether a given number is a power of 4 or not | Java program to check if given number is power of 4 or not ; Function to check if x is power of 4 ; Check if there is only one bit set in n ; count 0 bits before set bit ; If count is even then return true else false ; If there are more than 1 bit set then n is not a power of 4 ; Driver Code
import java . io . * ; class GFG { static int isPowerOfFour ( int n ) { int count = 0 ; int x = n & ( n - 1 ) ; if ( n > 0 && x == 0 ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % 2 == 0 ) ? 1 : 0 ; } return 0 ; } public static void main ( String [ ] args ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) > 0 ) System . out . println ( test_no + " ▁ is ▁ a ▁ power ▁ of ▁ 4" ) ; else System . out . println ( test_no + " ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" ) ; } }
Find whether a given number is a power of 4 or not | Java program to check if given number is power of 4 or not ; Driver Code
import java . io . * ; class GFG { static boolean isPowerOfFour ( int n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) && ( n & 0xAAAAAAAA ) == 0 ; } public static void main ( String [ ] args ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) ) System . out . println ( test_no + " ▁ is ▁ a ▁ power ▁ of ▁ 4" ) ; else System . out . println ( test_no + " ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" ) ; } }
Find whether a given number is a power of 4 or not | Java program to check if given number is power of 4 or not ; 0 is not considered as a power of 4 ; Driver code
import java . util . * ; class GFG { static double logn ( int n , int r ) { return Math . log ( n ) / Math . log ( r ) ; } static boolean isPowerOfFour ( int n ) { if ( n == 0 ) return false ; return Math . floor ( logn ( n , 4 ) ) == Math . ceil ( logn ( n , 4 ) ) ; } public static void main ( String [ ] args ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) ) System . out . print ( test_no + " ▁ is ▁ a ▁ power ▁ of ▁ 4" ) ; else System . out . print ( test_no + " ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" ) ; } }
Compute the minimum or maximum of two integers without branching | Java program to Compute the minimum or maximum of two integers without branching ; Function to find minimum of x and y ; Function to find maximum of x and y ; Driver program to test above functions
public class AWS { static int min ( int x , int y ) { return y ^ ( ( x ^ y ) & - ( x << y ) ) ; } static int max ( int x , int y ) { return x ^ ( ( x ^ y ) & - ( x << y ) ) ; } public static void main ( String [ ] args ) { int x = 15 ; int y = 6 ; System . out . print ( " Minimum ▁ of ▁ " + x + " ▁ and ▁ " + y + " ▁ is ▁ " ) ; System . out . println ( min ( x , y ) ) ; System . out . print ( " Maximum ▁ of ▁ " + x + " ▁ and ▁ " + y + " ▁ is ▁ " ) ; System . out . println ( max ( x , y ) ) ; } }
Compute the minimum or maximum of two integers without branching | JAVA implementation of above approach ; Function to find minimum of x and y ; Function to find maximum of x and y ; Driver code
class GFG { static int CHAR_BIT = 4 ; static int INT_BIT = 8 ; static int min ( int x , int y ) { return y + ( ( x - y ) & ( ( x - y ) >> ( INT_BIT * CHAR_BIT - 1 ) ) ) ; } static int max ( int x , int y ) { return x - ( ( x - y ) & ( ( x - y ) >> ( INT_BIT * CHAR_BIT - 1 ) ) ) ; } public static void main ( String [ ] args ) { int x = 15 ; int y = 6 ; System . out . println ( " Minimum ▁ of ▁ " + x + " ▁ and ▁ " + y + " ▁ is ▁ " + min ( x , y ) ) ; System . out . println ( " Maximum ▁ of ▁ " + x + " ▁ and ▁ " + y + " ▁ is ▁ " + max ( x , y ) ) ; } }
Compute the minimum or maximum of two integers without branching | Java program for the above approach ; absbit32 function ; max function ; min function ; Driver code
import java . io . * ; class GFG { public static int absbit32 ( int x , int y ) { int sub = x - y ; int mask = ( sub >> 31 ) ; return ( sub ^ mask ) - mask ; } public static int max ( int x , int y ) { int abs = absbit32 ( x , y ) ; return ( x + y + abs ) / 2 ; } public static int min ( int x , int y ) { int abs = absbit32 ( x , y ) ; return ( x + y - abs ) / 2 ; } public static void main ( String [ ] args ) { System . out . println ( max ( 2 , 3 ) ) ; System . out . println ( max ( 2 , - 3 ) ) ; System . out . println ( max ( - 2 , - 3 ) ) ; System . out . println ( min ( 2 , 3 ) ) ; System . out . println ( min ( 2 , - 3 ) ) ; System . out . println ( min ( - 2 , - 3 ) ) ; } }
Find the two non | Java Program for above approach ; This function sets the values of * x and * y to non - repeating elements in an array arr [ ] of size n ; Xor all the elements of the array all the elements occuring twice will cancel out each other remaining two unnique numbers will be xored ; Bitwise & the sum with it ' s ▁ 2' s Complement Bitwise & will give us the sum containing only the rightmost set bit ; sum1 and sum2 will contains 2 unique elements elements initialized with 0 box number xored with 0 is number itself ; traversing the array again ; Bitwise & the arr [ i ] with the sum Two possibilities either result == 0 or result > 0 ; if result > 0 then arr [ i ] xored with the sum1 ; if result == 0 then arr [ i ] xored with sum2 ; print the the two unique numbers ; Driver Code
public class UniqueNumbers { public static void UniqueNumbers2 ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = ( sum ^ arr [ i ] ) ; } sum = ( sum & - sum ) ; int sum1 = 0 ; int sum2 = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( ( arr [ i ] & sum ) > 0 ) { sum1 = ( sum1 ^ arr [ i ] ) ; } else { sum2 = ( sum2 ^ arr [ i ] ) ; } } System . out . println ( " The ▁ non - repeating ▁ elements ▁ are ▁ " + sum1 + " ▁ and ▁ " + sum2 ) ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 2 , 3 , 7 , 9 , 11 , 2 , 3 , 11 } ; int n = arr . length ; UniqueNumbers2 ( arr , n ) ; } }
Find the Number Occurring Odd Number of Times | Java program to find the element occurring odd number of times ; function to find the element occurring odd number of times ; driver code ; Function calling
class OddOccurrence { static int getOddOccurrence ( int arr [ ] , int arr_size ) { int i ; for ( i = 0 ; i < arr_size ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < arr_size ; j ++ ) { if ( arr [ i ] == arr [ j ] ) count ++ ; } if ( count % 2 != 0 ) return arr [ i ] ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 } ; int n = arr . length ; System . out . println ( getOddOccurrence ( arr , n ) ) ; } }
Find the Number Occurring Odd Number of Times | Java program to find the element occurring odd number of times ; function to find the element occurring odd number of times ; Putting all elements into the HashMap ; If array element is already present then increase the count of that element . ; if array element is not present then put element into the HashMap and initialize the count to one . ; Checking for odd occurrence of each element present in the HashMap ; driver code
import java . io . * ; import java . util . HashMap ; class OddOccurrence { static int getOddOccurrence ( int arr [ ] , int n ) { HashMap < Integer , Integer > hmap = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hmap . containsKey ( arr [ i ] ) ) { int val = hmap . get ( arr [ i ] ) ; hmap . put ( arr [ i ] , val + 1 ) ; } else hmap . put ( arr [ i ] , 1 ) ; } for ( Integer a : hmap . keySet ( ) ) { if ( hmap . get ( a ) % 2 != 0 ) return a ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 } ; int n = arr . length ; System . out . println ( getOddOccurrence ( arr , n ) ) ; } }
Count number of bits to be flipped to convert A to B | Count number of bits to be flipped to convert A into B ; Function that count set bits ; Function that return count of flipped number ; Return count of set bits in a XOR b ; Driver code
import java . util . * ; class Count { public static int countSetBits ( int n ) { int count = 0 ; while ( n != 0 ) { count ++ ; n &= ( n - 1 ) ; } return count ; } public static int FlippedCount ( int a , int b ) { return countSetBits ( a ^ b ) ; } public static void main ( String [ ] args ) { int a = 10 ; int b = 20 ; System . out . print ( FlippedCount ( a , b ) ) ; } }
Position of rightmost set bit | Java Code for Position of rightmost set bit ; Drive code
class GFG { public static int getFirstSetBitPos ( int n ) { return ( int ) ( ( Math . log10 ( n & - n ) ) / Math . log10 ( 2 ) ) + 1 ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( getFirstSetBitPos ( n ) ) ; } }
Position of rightmost set bit | Java program to find the first rightmost set bit using XOR operator ; function to find the rightmost set bit ; Position variable initialize with 1 m variable is used to check the set bit ; left shift ; Driver Code ; function call
class GFG { static int PositionRightmostSetbit ( int n ) { int position = 1 ; int m = 1 ; while ( ( n & m ) == 0 ) { m = m << 1 ; position ++ ; } return position ; } public static void main ( String [ ] args ) { int n = 16 ; System . out . println ( PositionRightmostSetbit ( n ) ) ; } }
Position of rightmost set bit | Java implementation of above approach ; counting the position of first set bit ; Driver code
public class GFG { static int INT_SIZE = 32 ; static int Right_most_setbit ( int num ) { int pos = 1 ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { if ( ( num & ( 1 << i ) ) == 0 ) pos ++ ; else break ; } return pos ; } public static void main ( String [ ] args ) { int num = 18 ; int pos = Right_most_setbit ( num ) ; System . out . println ( pos ) ; } }
Position of rightmost set bit | Java program for above approach ; Function to find position of rightmost set bit ; Iterate till number > 0 ; Checking if last bit is set ; Increment position and right shift number ; set bit not found . ; Driver Code ; Function call
import java . io . * ; class GFG { public static int Last_set_bit ( int n ) { int p = 1 ; while ( n > 0 ) { if ( ( n & 1 ) > 0 ) { return p ; } p ++ ; n = n >> 1 ; } return - 1 ; } public static void main ( String [ ] args ) { int n = 18 ; int pos = Last_set_bit ( n ) ; if ( pos != - 1 ) System . out . println ( pos ) ; else System . out . println ( "0" ) ; } }
Binary representation of a given number |  ; bin function ; Driver code
public class GFG { static void bin ( long n ) { long i ; System . out . print ( "0" ) ; for ( i = 1 << 30 ; i > 0 ; i = i / 2 ) { if ( ( n & i ) != 0 ) { System . out . print ( "1" ) ; } else { System . out . print ( "0" ) ; } } } public static void main ( String [ ] args ) { bin ( 7 ) ; System . out . println ( ) ; bin ( 4 ) ; } }
Binary representation of a given number | Java implementation of the approach ; Function to convert decimal to binary number ; Driver code
class GFG { static void bin ( Integer n ) { if ( n > 1 ) bin ( n >> 1 ) ; System . out . printf ( " % d " , n & 1 ) ; } public static void main ( String [ ] args ) { bin ( 131 ) ; System . out . printf ( "NEW_LINE"); bin ( 3 ) ; } }
Swap all odd and even bits | Java program to swap even and odd bits of a given number ; Function to swap even and odd bits ; Get all even bits of x ; Get all odd bits of x ; Right shift even bits ; Left shift odd bits ; Combine even and odd bits ; Driver program to test above function ; 00010111 ; Output is 43 ( 00101011 )
class GFG { static int swapBits ( int x ) { int even_bits = x & 0xAAAAAAAA ; int odd_bits = x & 0x55555555 ; even_bits >>= 1 ; odd_bits <<= 1 ; return ( even_bits odd_bits ) ; } public static void main ( String [ ] args ) { int x = 23 ; System . out . println ( swapBits ( x ) ) ; } }
Find position of the only set bit | Java program to find position of only set bit in a given number ; A utility function to check whether n is a power of 2 or not . goo . gl / 17 Arj See http : ; Returns position of the only set bit in ' n ' ; Iterate through bits of n till we find a set bit i & n will be non - zero only when ' i ' and ' n ' have a set bit at same position ; Unset current bit and set the next bit in ' i ' ; increment position ; Driver code
class GFG { static boolean isPowerOfTwo ( int n ) { return ( n > 0 && ( ( n & ( n - 1 ) ) == 0 ) ) ? true : false ; } static int findPosition ( int n ) { if ( ! isPowerOfTwo ( n ) ) return - 1 ; int i = 1 , pos = 1 ; while ( ( i & n ) == 0 ) { i = i << 1 ; ++ pos ; } return pos ; } public static void main ( String [ ] args ) { int n = 16 ; int pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; n = 12 ; pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; n = 128 ; pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; } }
Find position of the only set bit | Java program to find position of only set bit in a given number ; A utility function to check whether n is power of 2 or not ; Returns position of the only set bit in ' n ' ; One by one move the only set bit to right till it reaches end ; increment count of shifts ; Driver code
class GFG { static boolean isPowerOfTwo ( int n ) { return n > 0 && ( ( n & ( n - 1 ) ) == 0 ) ; } static int findPosition ( int n ) { if ( ! isPowerOfTwo ( n ) ) return - 1 ; int count = 0 ; while ( n > 0 ) { n = n >> 1 ; ++ count ; } return count ; } public static void main ( String [ ] args ) { int n = 0 ; int pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; n = 12 ; pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; n = 128 ; pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; } }
How to swap two numbers without using a temporary variable ? | Java Program to swap two numbers without using temporary variable ; Code to swap ' x ' and ' y ' x now becomes 50 ; y becomes 10 ; x becomes 5
import java . io . * ; class GFG { public static void main ( String [ ] args ) { int x = 10 ; int y = 5 ; x = x * y ; y = x / y ; x = x / y ; System . out . println ( " After ▁ swaping : " + " ▁ x ▁ = ▁ " + x + " , ▁ y ▁ = ▁ " + y ) ; } }