codeparrot/xlcost-text-to-code huggingface dataset:
Find the Majority Element | Set 3 ( Bit Magic ) |  ; Number of bits in the integer ; Variable to calculate majority element ; Loop to iterate through all the bits of number ; Loop to iterate through all elements in array to count the total set bit at position i from right ; If the total set bits exceeds n / 2 , this bit should be present in majority Element . ; Iterate through array get the count of candidate majority element ; Verify if the count exceeds n / 2 ; Driver Code
function findMajority ( arr , n ) { let len = 32 ; let number = 0 ; for ( let i = 0 ; i < len ; i ++ ) { let countt = 0 ; for ( let j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) != 0 ) countt ++ ; } if ( countt > parseInt ( n / 2 , 10 ) ) number += ( 1 << i ) ; } let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] == number ) count ++ ; if ( count > parseInt ( n / 2 , 10 ) ) document . write ( number ) ; else document . write ( " " ) ; } let arr = [ 3 , 3 , 4 , 2 , 4 , 4 , 2 , 4 , 4 ] ; let n = arr . length ; findMajority ( arr , n ) ;
Count number of bits to be flipped to convert A to B | Set | Function to return the count of bits to be flipped to convert a to b ; To store the required count ; Loop until both of them become zero ; Store the last bits in a as well as b ; If the current bit is not same in both the integers ; Right shift both the integers by 1 ; Return the count ; Driver code
function countBits ( a , b ) { var count = 0 ; while ( a b ) { var last_bit_a = a & 1 ; var last_bit_b = b & 1 ; if ( last_bit_a != last_bit_b ) count ++ ; a = a >> 1 ; b = b >> 1 ; } return count ; } var a = 10 , b = 7 ; document . write ( countBits ( a , b ) ) ;
Count Set | Recursive function to find number of set bist in a number ; Base condition ; If Least significant bit is set ; If Least significant bit is not set ; Driver code ; Function call
function CountSetBits ( n ) { if ( n == 0 ) return 0 ; if ( ( n & 1 ) == 1 ) return 1 + CountSetBits ( n >> 1 ) ; else return CountSetBits ( n >> 1 ) ; } var n = 21 ; document . write ( CountSetBits ( n ) ) ;
Sum of Bitwise OR of all pairs in a given array | Returns value of " arr [ 0 ] ▁ | ▁ arr [ 1 ] ▁ + ▁ arr [ 0 ] ▁ | ▁ arr [ 2 ] ▁ + ▁ . . . ▁ arr [ i ] ▁ | ▁ arr [ j ] ▁ + ▁ . . . . . ▁ arr [ n - 2 ] ▁ | ▁ arr [ n - 1 ] " ; Consider all pairs ( arr [ i ] , arr [ j ) such that i < j ; Driver program to test above function
function pairORSum ( arr , n ) { for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j < n ; j ++ ) ans += arr [ i ] | arr [ j ] ; return ans ; } var arr = [ 1 , 2 , 3 , 4 ] ; var n = arr . length ; document . write ( pairORSum ( arr , n ) ) ;
Number of 0 s and 1 s at prime positions in the given array | Function that returns true if n is prime ; Check from 2 to n ; Function to find the count of 0 s and 1 s at prime indices ; To store the count of 0 s and 1 s ; If current 0 is at prime position ; If current 1 is at prime position ; Driver code
function isPrime ( n ) { if ( n <= 1 ) return false ; for ( let i = 2 ; i < n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } function countPrimePosition ( arr , n ) { let c0 = 0 , c1 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 && isPrime ( i ) ) c0 ++ ; if ( arr [ i ] == 1 && isPrime ( i ) ) c1 ++ ; } document . write ( " " + c0 + " " ) ; document . write ( " " + c1 ) ; } let arr = [ 1 , 0 , 1 , 0 , 1 ] ; let n = arr . length ; countPrimePosition ( arr , n ) ;
Find a sub matrix with maximum XOR | Javascript program to implement the above approach ; Compute the xor of elements from ( 1 , 1 ) to ( i , j ) and store it in prefix_xor [ i ] [ j ] ; xor of submatrix from 1 , 1 to i , j is ( xor of submatrix from 1 , 1 to i - 1 , j ) ^ ( xor of submatrix from 1 , 1 to i , j - 1 ) ^ ( xor of submatrix from 1 , 1 to i - 1 , j - 1 ) ^ arr [ i ] [ j ] ; find the submatrix with maximum xor value ; we need four loops to find all the submatrix of a matrix ; xor of submatrix from i , j to i1 , j1 is ( xor of submatrix from 1 , 1 to i1 , j1 ) ^ ( xor of submatrix from 1 , 1 to i - 1 , j - 1 ) ^ ( xor of submatrix from 1 , 1 to i1 , j - 1 ) ^ ( xor of submatrix from 1 , 1 to i - 1 , j1 ) ; if the xor is greater than maximum value substitute it ; Driver code ; Find the prefix_xor ; Find submatrix with maximum bitwise xor
var N = 101 ; function prefix ( arr , prefix_xor , n ) { for ( var i = 1 ; i < n ; i ++ ) { for ( var j = 1 ; j < n ; j ++ ) { prefix_xor [ i ] [ j ] = arr [ i ] [ j ] ^ prefix_xor [ i - 1 ] [ j ] ^ prefix_xor [ i ] [ j - 1 ] ^ prefix_xor [ i - 1 ] [ j - 1 ] ; } } } function Max_xor ( prefix_xor , n ) { var max_value = 0 ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 1 ; j <= n ; j ++ ) { for ( var i1 = i ; i1 <= n ; i1 ++ ) { for ( var j1 = j ; j1 <= n ; j1 ++ ) { var x = 0 ; x ^= prefix_xor [ i1 ] [ j1 ] ; x ^= prefix_xor [ i - 1 ] [ j - 1 ] ; x ^= prefix_xor [ i1 ] [ j - 1 ] ; x ^= prefix_xor [ i - 1 ] [ j1 ] ; max_value = Math . max ( max_value , x ) ; } } } } document . write ( max_value ) ; } var arr = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] ; var n = 4 ; var prefix_xor = Array . from ( Array ( N ) , ( ) => Array ( N ) . fill ( 0 ) ) ; prefix ( arr , prefix_xor , n ) ; Max_xor ( prefix_xor , n ) ;
Multiply a number by 15 without using * and / operators | Function to return ( 15 * N ) without using ' * ' or ' / ' operator ; prod = 16 * n ; ( ( 16 * n ) - n ) = 15 * n ; Driver code
function multiplyByFifteen ( n ) { let prod = ( n << 4 ) ; prod = prod - n ; return prod ; } let n = 7 ; document . write ( multiplyByFifteen ( n ) ) ;
Multiply a number by 15 without using * and / operators | Function to return ( 15 * N ) without using ' * ' or ' / ' operator ; prod = 8 * n ; Add ( 4 * n ) ; Add ( 2 * n ) ; Add n ; ( 8 * n ) + ( 4 * n ) + ( 2 * n ) + n = ( 15 * n ) ; Driver code
function multiplyByFifteen ( n ) { var prod = ( n << 3 ) ; prod += ( n << 2 ) ; prod += ( n << 1 ) ; prod += n ; return prod ; } var n = 7 ; document . write ( multiplyByFifteen ( n ) ) ;
Find a number which give minimum sum when XOR with every number of array of integers | Function to find an integer X such that the sum of all the array elements after getting XORed with X is minimum ; Finding Maximum element of array ; Find Maximum number of bits required in the binary representation of maximum number so log2 is calculated ; Running loop from p times which is the number of bits required to represent all the elements of the array ; If the bits in same position are set then count ; If count becomes greater than half of size of array then we need to make that bit '0' by setting X bit to '1' ; Again using shift operation to calculate the required number ; Calculate minimized sum ; Print solution ; Driver code
function findX ( arr , n ) { let itr = Math . max ( ... arr ) ; let p = parseInt ( Math . log ( itr ) / Math . log ( 2 ) ) + 1 ; let X = 0 ; for ( let i = 0 ; i < p ; i ++ ) { let count = 0 ; for ( let j = 0 ; j < n ; j ++ ) { if ( arr [ j ] & ( 1 << i ) ) { count ++ ; } } if ( count > parseInt ( n / 2 ) ) { X += 1 << i ; } } let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += ( X ^ arr [ i ] ) ; document . write ( " " + X + " " + sum ) ; } let arr = [ 2 , 3 , 4 , 5 , 6 ] ; let n = arr . length ; findX ( arr , n ) ;
Game Theory in Balanced Ternary Numeral System | ( Moving 3 k steps at a time ) | Numbers are in range of pow ( 3 , 32 ) ; Conversion of ternary into balanced ternary as start iterating from Least Significant Bit ( i . e 0 th ) , if encountered 0 or 1 , safely skip and pass carry 0 further 2 , replace it to - 1 and pass carry 1 further 3 , replace it to 0 and pass carry 1 further ; Similar to binary conversion ; Driver code ; Moving on to first occupied bit ; Printing ; Print ' Z ' in place of - 1
let arr = new Array ( 33 ) ; function balTernary ( ter ) { let carry = 0 , base = 10 ; let i = 32 ; while ( ter > 0 ) { let rem = ter % base ; rem = rem + carry ; if ( rem == 0 ) { arr [ i -- ] = 0 ; carry = 0 ; } else if ( rem == 1 ) { arr [ i -- ] = 1 ; carry = 0 ; } else if ( rem == 2 ) { arr [ i -- ] = - 1 ; carry = 1 ; } else if ( rem == 3 ) { arr [ i -- ] = 0 ; carry = 1 ; } ter = Math . floor ( ter / base ) ; } if ( carry == 1 ) arr [ i ] = 1 ; } function ternary ( number ) { let ans = 0 , rem = 1 , base = 1 ; while ( number > 0 ) { rem = number % 3 ; ans = ans + rem * base ; number = Math . floor ( number / 3 ) ; base = base * 10 ; } return ans ; } let number = 3056 ; let ter = ternary ( number ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = 0 ; } balTernary ( ter ) ; let i = 0 ; while ( arr [ i ] == 0 ) { i ++ ; } for ( let j = i ; j <= 32 ; j ++ ) { if ( arr [ j ] == - 1 ) document . write ( ' ' ) ; else document . write ( arr [ j ] ) ; }
Minimum value among AND of elements of every subset of an array | Javascript implementation of the approach ; Find AND of whole array ; Prlet the answer ; Driver code
function minAND ( arr , n ) { let s = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { s = s & arr [ i ] ; } document . write ( ( s ) + " " ) ; } let arr = [ 1 , 2 , 3 ] ; let n = arr . length ; minAND ( arr , n ) ;
Modify a binary array to Bitwise AND of all elements as 1 | Function to check if it is possible or not ; Driver code
function check ( a , n ) { for ( var i = 0 ; i < n ; i ++ ) if ( a [ i ] ) return true ; return false ; } var a = [ 0 , 1 , 0 , 1 ] ; var n = a . length ; check ( a , n ) ? document . write ( " " ) : document . write ( " " ) ;
Find array using different XORs of elements in groups of size 4 | Utility function to print the contents of the array ; Function to find the required array ; Print the array ; Driver code
function printArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function findArray ( q , n ) { let arr = new Array ( n ) , ans ; for ( let k = 0 , j = 0 ; j < parseInt ( n / 4 ) ; j ++ ) { ans = q [ k ] ^ q [ k + 3 ] ; arr [ k + 1 ] = q [ k + 1 ] ^ ans ; arr [ k + 2 ] = q [ k + 2 ] ^ ans ; arr [ k ] = q [ k ] ^ ( ( arr [ k + 1 ] ) ^ ( arr [ k + 2 ] ) ) ; arr [ k + 3 ] = q [ k + 3 ] ^ ( arr [ k + 1 ] ^ arr [ k + 2 ] ) ; k += 4 ; } printArray ( arr , n ) ; } let q = [ 4 , 1 , 7 , 0 ] ; let n = q . length ; findArray ( q , n ) ;
Count of values of x <= n for which ( n XOR x ) = ( n | Function to return the count of valid values of x ; Convert n into binary String ; To store the count of 1 s ; If current bit is 1 ; Calculating answer ; Driver code
function countX ( n ) { let binary = ( n >>> 0 ) . toString ( 2 ) ; let count = 0 ; for ( let i = 0 ; i < binary . length ; i ++ ) { if ( binary [ i ] == ' ' ) count ++ ; } let answer = Math . floor ( Math . pow ( 2 , count ) ) ; return answer ; } let n = 5 ; let answer = countX ( n ) ; document . write ( answer ) ;
Check if the binary representation of a number has equal number of 0 s and 1 s in blocks | function to convert decimal to binary ; Count same bits in last block ; If n is 0 or it has all 1 s , then it is not considered to have equal number of 0 s and 1 s in blocks . ; Count same bits in all remaining blocks . ; Driver code
function isEqualBlock ( n ) { var first_bit = n % 2 ; var first_count = 1 ; n = n / 2 ; while ( n % 2 == first_bit && n > 0 ) { n = n / 2 ; first_count ++ ; } if ( n == 0 ) return false ; while ( n > 0 ) { first_bit = n % 2 ; var curr_count = 1 ; n = n / 2 ; while ( n % 2 == first_bit ) { n = n / 2 ; curr_count ++ ; } if ( curr_count != first_count ) return false ; } return true ; } var n = 51 ; if ( isEqualBlock ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
First and Last Three Bits | Function to print the first and last 3 bits equivalent decimal number ; Converting n to binary ; Length of the array has to be at least 3 ; Convert first three bits to decimal ; Print the decimal ; Convert last three bits to decimal ; Print the decimal ; Driver code
function binToDecimal3 ( n ) { var a = Array ( 64 ) . fill ( 0 ) ; var x = 0 , i ; for ( i = 0 ; n > 0 ; i ++ ) { a [ i ] = n % 2 ; n = parseInt ( n / 2 ) ; } x = ( i < 3 ) ? 3 : i ; var d = 0 , p = 0 ; for ( var i = x - 3 ; i < x ; i ++ ) d += a [ i ] * parseInt ( Math . pow ( 2 , p ++ ) ) ; document . write ( d + " " ) ; d = 0 ; p = 0 ; for ( var i = 0 ; i < 3 ; i ++ ) d += a [ i ] * parseInt ( Math . pow ( 2 , p ++ ) ) ; document . write ( d ) ; } var n = 86 ; binToDecimal3 ( n ) ;
First and Last Three Bits | Function to print the first and last 3 bits equivalent decimal number ; Number formed from last three bits ; Let us get first three bits in n ; Number formed from first three bits ; Printing result ; Driver code
function binToDecimal3 ( n ) { var last_3 = ( ( n & 4 ) + ( n & 2 ) + ( n & 1 ) ) ; n = n >> 3 ; while ( n > 7 ) n = n >> 1 ; var first_3 = ( ( n & 4 ) + ( n & 2 ) + ( n & 1 ) ) ; document . write ( first_3 + " " + last_3 ) ; } var n = 86 ; binToDecimal3 ( n ) ;
Count of numbers which can be made power of 2 by given operation | Function that returns true if x is a power of 2 ; If x & ( x - 1 ) = 0 then x is a power of 2 ; Function to return the required count ; If a [ i ] or ( a [ i ] + 1 ) is a power of 2 ; Driver code
function isPowerOfTwo ( x ) { if ( x == 0 ) return false ; if ( ( x & ( x - 1 ) ) == 0 ) return true ; else return false ; } function countNum ( a , n ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( isPowerOfTwo ( a [ i ] ) || isPowerOfTwo ( a [ i ] + 1 ) ) count ++ ; } return count ; } let arr = [ 5 , 6 , 9 , 3 , 1 ] ; let n = arr . length ; document . write ( countNum ( arr , n ) ) ;
Sum of elements from an array having even parity | Function that returns true if x has even parity ; We basically count set bits https : www . geeksforgeeks . org / count - set - bits - in - an - integer / ; Function to return the sum of the elements from an array which have even parity ; If a [ i ] has even parity ; Driver code
function checkEvenParity ( x ) { let parity = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; parity ++ ; } if ( parity % 2 == 0 ) return true ; else return false ; } function sumlist ( a , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( checkEvenParity ( a [ i ] ) ) sum += a [ i ] ; } return sum ; } let arr = [ 2 , 4 , 3 , 5 , 9 ] ; let n = arr . length ; document . write ( sumlist ( arr , n ) ) ;
Number of pairs with Bitwise OR as Odd number | Function to count pairs with odd OR ; find OR operation check odd or odd ; return count of odd pair ; Driver Code
function findOddPair ( A , N ) { let oddPair = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] A [ j ] ) % 2 != 0 ) oddPair ++ ; } } return oddPair ; } let A = [ 5 , 6 , 2 , 8 ] ; let N = A . length ; document . write ( findOddPair ( A , N ) ) ;
Count pairs with Bitwise XOR as EVEN number | Function to count number of even pairs ; variable for counting even pairs ; find all pairs ; find XOR operation check even or even ; return number of even pair ; Driver Code ; calling function findevenPair and print number of even pair
function findevenPair ( A , N ) { let i , j ; let evenPair = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] ^ A [ j ] ) % 2 == 0 ) evenPair ++ ; } } return evenPair ; } let A = [ 5 , 4 , 7 , 2 , 1 ] ; let N = A . length ; document . write ( findevenPair ( A , N ) ) ;
Count pairs with Bitwise XOR as EVEN number | Function to count number of even pairs ; find all pairs ; return number of even pair ; Driver Code ; calling function findEvenPair and print number of even pair
function findEvenPair ( A , N ) { let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 != 0 ) count ++ ; } let totalPairs = parseInt ( N * ( N - 1 ) / 2 ) ; let oddEvenPairs = count * ( N - count ) ; return totalPairs - oddEvenPairs ; } let a = [ 5 , 4 , 7 , 2 , 1 ] ; let n = a . length ; document . write ( findEvenPair ( a , n ) ) ;
Count pairs with Bitwise | Function to count number of pairs EVEN bitwise AND ; variable for counting even pairs ; find all pairs ; find AND operation to check evenpair ; return number of even pair ; Driver Code
function findevenPair ( A , N ) { let i , j ; let evenPair = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] & A [ j ] ) % 2 == 0 ) evenPair ++ ; } } return evenPair ; } let a = [ 5 , 1 , 3 , 2 ] ; let n = a . length ; document . write ( findevenPair ( a , n ) ) ;
Count pairs with Bitwise | Function to count number of pairs with EVEN bitwise AND ; Count odd numbers ; Count odd pairs ; Return number of even pair ; Driver Code
function findevenPair ( A , N ) { let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) if ( A [ i ] % 2 != 0 ) count ++ ; let oddCount = parseInt ( ( count * ( count - 1 ) ) / 2 ) ; return parseInt ( ( N * ( N - 1 ) ) / 2 ) - oddCount ; } let a = [ 5 , 1 , 3 , 2 ] ; let n = a . length ; document . write ( findevenPair ( a , n ) ) ;
Find a value whose XOR with given number is maximum | Function To Calculate Answer ; Find number of bits in the given integer ; XOR the given integer with poe ( 2 , number_of_bits - 1 and print the result ; Driver Code
function calculate ( X ) { let number_of_bits = 8 ; return ( ( 1 << number_of_bits ) - 1 ) ^ X ; } let X = 4 ; document . write ( " " + calculate ( X ) + " " ) ;
XOR of all elements of array with set bits equal to K | Function to find Xor of desired elements ; Initialize vector ; push required elements ; Initialize result with first element of vector ; Driver code
function xorGivenSetBits ( arr , n , k ) { let v = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( bitcount ( arr [ i ] ) == k ) { v . push ( arr [ i ] ) ; } } let result = v [ 0 ] ; for ( let i = 1 ; i < v . length ; i ++ ) result = result ^ v [ i ] ; return result ; } function bitcount ( n ) { var count = 0 ; while ( n ) { count += n & 1 ; n >>= 1 ; } return count ; } let arr = [ 2 , 13 , 1 , 19 , 7 ] ; let n = arr . length ; let k = 3 ; document . write ( xorGivenSetBits ( arr , n , k ) ) ;
Replace every element of the array with BitWise XOR of all other | Function to replace the elements ; Calculate the xor of all the elements ; Replace every element by the xor of all other elements ; Driver code ; Print the modified array .
function ReplaceElements ( arr , n ) { let X = 0 ; for ( let i = 0 ; i < n ; ++ i ) { X ^= arr [ i ] ; } for ( let i = 0 ; i < n ; ++ i ) { arr [ i ] = X ^ arr [ i ] ; } } let arr = [ 2 , 3 , 3 , 5 , 5 ] ; let n = arr . length ; ReplaceElements ( arr , n ) ; for ( let i = 0 ; i < n ; ++ i ) { document . write ( arr [ i ] + " " ) ; }
Find consecutive 1 s of length >= n in binary representation of a number | Function to count the number of leading zeros ; Function to find the string of n consecutive 1 's ; Initialize position to return . ; Skip leading 0 's ; Set position after leading 0 's ; Count first group of 1 's. ; If length of consecutive 1 's  is greater than or equal to n ; Not enough 1 's  skip over to next group ; Update the position ; if no string is found ; Driver code
function countLeadingZeros ( x ) { let y ; let n ; 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 ; } function FindStringof1s ( x , n ) { let k , p ; p = 0 ; while ( x != 0 ) { k = countLeadingZeros ( x ) ; x = x << k ; p = p + k ; k = countLeadingZeros ( ~ x ) ; if ( k >= n ) return p + 1 ; x = x << k ; p = p + k ; } return - 1 ; } let x = 35 ; let n = 2 ; document . write ( FindStringof1s ( x , n ) ) ;
Assign other value to a variable from two possible values | Function to alternate the values ; Main function
function alternate ( a , b , x ) { return x = a ^ b ^ x ; } var a = - 10 ; var b = 15 ; var x = a ; document . write ( " " + x ) ; x = alternate ( a , b , x ) ; document . write ( " " ) ; document . write ( " " + x ) ;
Number of leading zeros in binary representation of a given number | JavaScript 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. ; Driver Code
let sizeofInt = 8 ; function countZeros ( x ) { let total_bits = sizeofInt * 8 ; let res = 0 ; while ( ( x & ( 1 << ( total_bits - 1 ) ) ) == 0 ) { x = ( x << 1 ) ; res ++ ; } return res ; } let x = 101 ; document . write ( countZeros ( x ) ) ;
Number of leading zeros in binary representation of a given number | Function to count the no . of leading zeros ; Main function
function countZeros ( x ) { let y ; let 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 ; } let x = 101 ; document . write ( countZeros ( x ) ) ;
Comparing leading zeros in binary representations of two numbers | Function to compare the no . of leading zeros ; if both have same no . of leading zeros ; if y has more leading zeros ; Main Function
function LeadingZeros ( x , y ) { if ( ( x ^ y ) <= ( x & y ) ) document . write ( " " ) ; else if ( ( x & ( ~ y ) ) > y ) document . write ( y ) ; else document . write ( x ) ; } let x = 10 , y = 16 ; LeadingZeros ( x , y ) ;
Printing all subsets of { 1 , 2 , 3 , ... n } without using array or loop | 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
function printSubsets ( numOfBits , num ) { if ( num >= 0 ) { document . write ( " " ) ; subset ( numOfBits - 1 , num , numOfBits ) ; document . write ( " " ) ; printSubsets ( numOfBits , num - 1 ) ; } else return ; } function subset ( nthBit , num , numOfBits ) { if ( nthBit >= 0 ) { if ( num & ( 1 << nthBit ) ) { document . write ( numOfBits - nthBit + " " ) ; } subset ( nthBit - 1 , num , numOfBits ) ; } else return ; } var n = 4 ; printSubsets ( n , Math . pow ( 2 , n ) - 1 ) ;
Number of mismatching bits in the binary representation of two integers | 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
function solve ( A , B ) { var count = 0 ; for ( i = 0 ; i < 32 ; i ++ ) { if ( ( ( A >> i ) & 1 ) != ( ( B >> i ) & 1 ) ) { count ++ ; } } document . write ( " " + count ) ; } var A = 12 , B = 15 ; solve ( A , B ) ;
Set the rightmost off bit | JavaScript program to set the rightmost unset bit ; If all bits are set ; Set rightmost 0 bit ; Driver program to test above
function setRightmostUnsetBit ( n ) { if ( ( n & ( n + 1 ) ) == 0 ) return n ; return n | ( n + 1 ) ; } let n = 21 ; document . write ( setRightmostUnsetBit ( n ) ) ;