codeparrot/xlcost-text-to-code huggingface dataset:
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
function rearrange ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) arr [ i ] += ( arr [ arr [ i ] ] % n ) * n ; for ( let i = 0 ; i < n ; i ++ ) arr [ i ] = Math . floor ( arr [ i ] / n ) ; } function printArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; } let arr = [ 3 , 2 , 0 , 1 ] ; let n = arr . length ; document . write ( " " ) ; printArr ( arr , n ) ; rearrange ( arr , n ) ; document . write ( " " ) ; printArr ( arr , n ) ;
Print all sequences of given length | 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
function printArray ( arr , size ) { for ( i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; return ; } function printSequencesRecur ( arr , n , k , index ) { var 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 ) ; } } } function printSequences ( n , k ) { var arr = Array . from ( { length : k } , ( _ , i ) => 0 ) ; printSequencesRecur ( arr , n , k , 0 ) ; return ; } var n = 3 ; var k = 2 ; printSequences ( n , k ) ;
Check if a number is multiple of 5 without using / and % operators | assumes that n is a positive integer ; Driver Code
function isMultipleof5 ( n ) { while ( n > 0 ) n = n - 5 ; if ( n == 0 ) return true ; return false ; } let n = 19 ; if ( isMultipleof5 ( n ) == true ) document . write ( n + " " ) ; else document . write ( n + " " ) ;
Count pairs from an array having sum of twice of their AND and XOR equal to K | 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 ; Given array ; Size of the array ; Given value of K
function countPairs ( arr , N , K ) { let mp = new Map ( ) ; let cnt = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( mp . has ( K - arr [ i ] ) ) { cnt += mp . get ( K - arr [ i ] ) ; } if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } document . write ( cnt ) ; } let arr = [ 1 , 5 , 4 , 8 , 7 ] ; let N = arr . length ; let K = 9 ; countPairs ( arr , N , K ) ;
Queries to count array elements from a given range having a single set bit | 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 ; Given array ; Size of the array ; Given queries ; Size of queries array
function check ( x ) { if ( ( ( x ) & ( x - 1 ) ) == 0 ) return 1 ; return 0 ; } function query ( l , r , pre ) { if ( l == 0 ) return pre [ r ] ; else return pre [ r ] - pre [ l - 1 ] ; } function countInRange ( arr , N , queries , Q ) { var pre = Array ( N ) . fill ( 0 ) ; pre [ 0 ] = check ( arr [ 0 ] ) ; for ( var i = 1 ; i < N ; i ++ ) { pre [ i ] = pre [ i - 1 ] + check ( arr [ i ] ) ; } var c = 0 ; while ( Q -- ) { var l = queries [ 0 ] ; var r = queries [ 1 ] ; c ++ ; document . write ( query ( l , r , pre ) + ' ' ) ; } } var arr = [ 12 , 11 , 16 , 8 , 2 , 5 , 1 , 3 , 256 , 1 ] ; var N = arr . length ; var queries = [ [ 0 , 9 ] , [ 4 , 9 ] ] ; var Q = queries . length ; countInRange ( arr , N , queries , Q ) ;
Bitwise operations on Subarrays 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 ; Given array [ ] arr ; Given subarray size K ; Function call
function build_num ( bit , k ) { let ans = 0 ; for ( let i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] == k ) ans += ( 1 << i ) ; return ans ; } function minimumAND ( arr , n , k ) { let bit = new Array ( 32 ) ; bit . fill ( 0 ) ; for ( let i = 0 ; i < k ; i ++ ) { for ( let j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } } let min_and = build_num ( bit , k ) ; for ( let i = k ; i < n ; i ++ ) { for ( let j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i - k ] & ( 1 << j ) ) > 0 ) bit [ j ] -- ; } for ( let 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 ; } let arr = [ 2 , 5 , 3 , 6 , 11 , 13 ] ; let k = 3 ; let n = arr . length ; document . write ( minimumAND ( arr , n , k ) ) ;
Equal Sum and XOR of three Numbers | 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
function calculate ( bit_cnt ) { let res = 1 ; while ( bit_cnt -- ) { res = res * 3 ; } return res ; } function unset_bit_count ( n ) { let count = 0 ; while ( n ) { if ( ( n & 1 ) == 0 ) count ++ ; n = n >> 1 ; } return count ; } let n ; n = 2 ; let count = unset_bit_count ( n ) ; let ans = calculate ( count ) ; document . write ( ans ) ;
Count pairs of elements such that number of set bits in their AND is B [ i ] | 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 ] ; Function to get no of set bits in binary representation of positive integer n ; Driver code
function solve ( A , B , n ) { var cnt = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i ; j < n ; j ++ ) { if ( countSetBits ( A [ i ] & A [ j ] ) == B [ j ] ) { cnt ++ ; } } } return cnt ; } function countSetBits ( n ) { var count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } var A = [ 2 , 3 , 1 , 4 , 5 ] ; var B = [ 2 , 2 , 1 , 4 , 2 ] ; var size = A . length ; document . write ( solve ( A , B , size ) ) ;
Total pairs in an array such that the bitwise AND , bitwise OR and bitwise XOR of LSB is 1 | 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
function CalculatePairs ( a , n ) { let cnt_zero = 0 ; let cnt_one = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 0 ) cnt_zero += 1 ; else cnt_one += 1 ; } let total_XOR_pairs = cnt_zero * cnt_one ; let total_AND_pairs = ( cnt_one ) * ( cnt_one - 1 ) / 2 ; let total_OR_pairs = cnt_zero * cnt_one + ( cnt_one ) * ( cnt_one - 1 ) / 2 ; document . write ( " " + total_XOR_pairs + " " ) ; document . write ( " " + total_AND_pairs + " " ) ; document . write ( " " + total_OR_pairs + " " ) ; } let a = [ 1 , 3 , 4 , 2 ] ; let n = a . length ; CalculatePairs ( a , n ) ;
Assign other value to a variable from two possible values | Function to alternate the values ; Main function
function alternate ( a , b , x ) { x = a + b - x ; document . write ( " " + " " + " " + x ) ; } var a = - 10 ; var b = 15 ; var x = a ; document . write ( " " + x + " " ) ; alternate ( a , b , x ) ;
Highest power of two that divides a given number | javascript program to find highest power of 2 that divides n . ; Driver code
function highestPowerOf2 ( n ) { return ( n & ( ~ ( n - 1 ) ) ) ; } var n = 48 ; document . write ( highestPowerOf2 ( n ) ) ;
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
function isSubsetAndZero ( array , len , N ) { var arrAnd = array [ 0 ] ; for ( var i = 1 ; i < len ; i ++ ) { arrAnd = arrAnd & array [ i ] ; } if ( ( arrAnd & N ) == 0 ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } var array = [ 1 , 2 , 4 ] ; var len = array . length ; var N = 3 ; isSubsetAndZero ( array , len , N ) ;
Finding the Parity of a number Efficiently | 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
function findParity ( x ) { let 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 ; } ( findParity ( 9 ) == 0 ) ? document . write ( " " ) : document . write ( " " ) ; ( findParity ( 13 ) == 0 ) ? document . write ( " " ) : document . write ( " " ) ;
Check if bits in range L to R of two numbers are complement of each other or not | 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
function allBitsSetInTheGivenRange ( n , l , r ) { var num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; var new_num = n & num ; if ( num == new_num ) return true ; return false ; } function bitsAreComplement ( a , b , l , r ) { var xor_value = a ^ b ; return allBitsSetInTheGivenRange ( xor_value , l , r ) ; } var a = 10 , b = 5 ; var l = 1 , r = 3 ; if ( bitsAreComplement ( a , b , l , r ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of the series 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + ... . . + 2 ^ n | 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
function calculateSum ( n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum = sum + ( 1 << i ) ; } return sum ; } let n = 10 ; document . write ( " " + 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
function printCombinations ( a , n , m ) { for ( let i = 0 ; i < ( 1 << n ) ; i ++ ) { let sum = 0 ; let num = 1 << ( n - 1 ) ; for ( let 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 ( let j = 0 ; j < n ; j ++ ) { if ( ( i & num ) > 0 ) document . write ( " " + a [ j ] + " " ) ; else document . write ( " " + a [ j ] + " " ) ; num = num >> 1 ; } document . write ( " " ) ; } } } let a = [ 3 , 5 , 6 , 8 ] ; let n = a . length ; let m = 5 ; printCombinations ( a , n , m ) ;
Same Number Of Set Bits As N | returns number of set bits in a number ; 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
function __builtin_popcount ( n ) { var d , t = 0 ; while ( n > 0 ) { d = n % 2 ; n = parseInt ( n / 2 ) ; if ( d == 1 ) t ++ ; } return t ; } function smallerNumsWithSameSetBits ( n ) { var temp = __builtin_popcount ( n ) ; var count = 0 ; for ( var i = n - 1 ; i > 0 ; i -- ) { if ( temp == __builtin_popcount ( i ) ) count ++ ; } return count ; } var n = 4 ; document . write ( smallerNumsWithSameSetBits ( n ) ) ;
Multiply any 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
function multiplyWith4 ( n ) { return ( n << 2 ) ; } var n = 4 ; document . write ( multiplyWith4 ( n ) ) ;
Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs | Javascript 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
let N = 1000 ; let tree = new Array ( N ) ; tree . fill ( 0 ) ; let level = new Array ( N ) ; level . fill ( 0 ) ; function constructTree ( low , high , pos , a ) { if ( low == high ) { level [ pos ] = 0 ; tree [ pos ] = a [ high ] ; return ; } let mid = parseInt ( ( low + high ) / 2 , 10 ) ; 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 ] ; } function update ( low , high , pos , index , a ) { if ( low == high && low == index ) { tree [ pos ] = a [ low ] ; return ; } if ( index < low index > high ) return ; if ( low != high ) { let mid = parseInt ( ( low + high ) / 2 , 10 ) ; 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 ] ; } } function updateValue ( index , value , a , n ) { a [ index ] = value ; update ( 0 , n - 1 , 0 , index , a ) ; static public void Main ( ) { } let a = [ 1 , 4 , 5 , 6 ] ; let n = a . length ; ; constructTree ( 0 , n - 1 , 0 , a ) ; let index = 0 ; let value = 2 ; updateValue ( index , value , a , n ) ; document . write ( tree [ 0 ] + " " ) ; index = 3 ; value = 5 ; updateValue ( index , value , a , n ) ; document . write ( tree [ 0 ] ) ;
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
function evenbitsetnumber ( n ) { let count = 0 , res = 0 ; for ( let temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 1 ) res |= ( 1 << count ) ; count ++ ; } return ( n res ) ; } let n = 10 ; document . write ( evenbitsetnumber ( n ) ) ;
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
function getmsb ( n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( n + 1 ) >> 1 ; } function getevenbits ( 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 ; } function setallevenbits ( n ) { return n | getevenbits ( n ) ; } let n = 10 ; document . write ( setallevenbits ( n ) ) ;
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
function oddbitsetnumber ( n ) { let count = 0 ; let res = 0 ; for ( let temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 0 ) res |= ( 1 << count ) ; count ++ ; } return ( n res ) ; } let n = 10 ; document . write ( oddbitsetnumber ( n ) ) ;
Set all odd bits of a 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
function getmsb ( n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( n + 1 ) >> 1 ; } function getevenbits ( 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 ; } function setalloddbits ( n ) { return n | getevenbits ( n ) ; } let n = 10 ; document . write ( setalloddbits ( n ) ) ;
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
function printNumHavingAltBitPatrn ( n ) { var curr_num = 1 ; document . write ( curr_num + " " ) ; while ( true ) { curr_num <<= 1 ; if ( n < curr_num ) break ; document . write ( curr_num + " " ) ; curr_num = ( ( curr_num ) << 1 ) ^ 1 ; if ( n < curr_num ) break ; document . write ( curr_num + " " ) ; } } var n = 50 ; printNumHavingAltBitPatrn ( n ) ;
Smallest perfect power of 2 greater than n ( without using arithmetic operators ) | 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
function perfectPowerOf2 ( n ) { let per_pow = 1 ; while ( n > 0 ) { per_pow = per_pow << 1 ; n = n >> 1 ; } return per_pow ; } let n = 128 ; document . write ( " " + n + " " + perfectPowerOf2 ( n ) ) ;
Find Unique pair in an array with pairs of numbers | Javascript program to find a unique pair in an array of pairs . ; XOR each element and get XOR of two unique elements ( ans ) ; Get a set bit of XOR ( We get the rightmost set bit ) ; Now divide elements in two sets by comparing rightmost set bit of XOR with bit at same position in each element . Initialize missing numbers ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; driver function
function findUniquePair ( arr , n ) { let XOR = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) XOR = XOR ^ arr [ i ] ; let set_bit_no = XOR & ~ ( XOR - 1 ) ; let x = 0 , y = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & set_bit_no ) > 0 ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } document . write ( " " + x + " " + y + " " + " " ) ; } let a = [ 6 , 1 , 3 , 5 , 1 , 3 , 7 , 6 ] ; let n = a . length ; findUniquePair ( a , n ) ;
Count unset bits of a number | JavaScript program to count unset bits in an integer . ; This makes sure two bits ( From MSB and including MSB ) are set ; This makes sure 4 bits ( From MSB and including MSB ) are set ; Count set bits in toggled number ; To store the count of set bits ; Driver Code
function countUnsetBits ( n ) { let x = n ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return BitCount ( x ^ n ) ; } function BitCount ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } let n = 17 ; document . write ( countUnsetBits ( n ) ) ;
Count total bits in a number | Function to get no of bits in binary representation of positive integer ; Driver Code
function countBits ( n ) { var count = 0 ; while ( n != 0 ) { count ++ ; n >>= 1 ; } return count ; } var i = 65 ; document . write ( countBits ( i ) ) ;
Toggle all bits after most significant bit | Function to toggle bits starting from MSB ; Temporary variable to use XOR with one of a n ; Run loop until the only set bit in temp crosses MST of n . ; Toggle bit of n corresponding to current set bit in temp . ; Move set bit to next higher position . ; Driver code
function toggle ( n ) { let temp = 1 ; while ( temp <= n ) { n = n ^ temp ; temp = temp << 1 ; } return n ; } let n = 10 ; n = toggle ( n ) ; document . write ( n ) ;
Find the n | Javascript program to find n - th number whose binary representation is palindrome . ; Finds if the kth bit is set in the binary representation ; Returns the position of leftmost set bit in the binary representation ; Finds whether the integer in binary representation is palindrome or not ; One by one compare bits ; Compare left and right bits and converge ; Start from 1 , traverse through all the integers ; If we reach n , break the loop ; Driver code ; Function Call
let INT_MAX = 2147483647 ; function isKthBitSet ( x , k ) { return ( ( x & ( 1 << ( k - 1 ) ) ) > 0 ) ? 1 : 0 ; } function leftmostSetBit ( x ) { let count = 0 ; while ( x > 0 ) { count ++ ; x = x >> 1 ; } return count ; } function isBinPalindrome ( x ) { let l = leftmostSetBit ( x ) ; let r = 1 ; while ( l > r ) { if ( isKthBitSet ( x , l ) != isKthBitSet ( x , r ) ) return 0 ; l -- ; r ++ ; } return 1 ; } function findNthPalindrome ( n ) { let pal_count = 0 ; let i = 0 ; for ( i = 1 ; i <= INT_MAX ; i ++ ) { if ( isBinPalindrome ( i ) > 0 ) { pal_count ++ ; } if ( pal_count == n ) break ; } return i ; } let n = 9 ; document . write ( findNthPalindrome ( n ) ) ;
Print ' K ' th least significant bit of a number | Function returns 1 if set , 0 if not ; Driver code ; Function call
function LSB ( num , K ) { let x = ( num & ( 1 << ( K - 1 ) ) ) != 0 ; return ( x ) ; } let num = 10 , K = 4 ; if ( LSB ( num , K ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if two numbers are equal without using comparison operators | Finds if a and b are same ; Driver code
function areSame ( a , b ) { if ( ( a - b ) == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } areSame ( 10 , 20 ) ;
Toggle bits in the given range | function to toggle bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; toggle bits in the range l to r in ' n ' and return the number Besides this , we can calculate num as : num = ( 1 << r ) - l . ; Driver program to test above
function toggleBitsFromLToR ( n , l , r ) { var num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return ( n ^ num ) ; } var n = 50 ; var l = 2 , r = 5 ; document . write ( toggleBitsFromLToR ( n , l , r ) ) ;
Position of rightmost different bit | Function to find the position of rightmost set bit in ' n ' returns 0 if there is no set bit . ; to handle edge case when n = 0. ; Function to find the position of rightmost different bit in the binary representations of ' m ' and ' n ' returns 0 if there is no rightmost different bit . ; position of rightmost different bit ; Driver program
function getRightMostSetBit ( n ) { if ( n == 0 ) return 0 ; return Math . log2 ( n & - n ) + 1 ; } function posOfRightMostDiffBit ( m , n ) { return getRightMostSetBit ( m ^ n ) ; } let m = 52 , n = 24 ; document . write ( " " + posOfRightMostDiffBit ( m , n ) + " " ) ;