codeparrot/xlcost-text-to-code huggingface dataset:
Check whether all the bits are unset in the given range or not | function to check whether all the bits are unset 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 new num is 0 , then all bits are unset in the given range ; else all bits are not unset ; Driver program to test above
function allBitsSetInTheGivenRange ( n , l , r ) { var num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; var new_num = n & num ; if ( new_num == 0 ) return " " ; return " " ; } var n = 17 ; var l = 2 , r = 4 ; document . write ( allBitsSetInTheGivenRange ( n , l , r ) ) ;
Toggle all bits after most significant bit | Returns a number which has all set bits starting from MSB of n ; This makes sure two bits ( From MSB and including MSB ) are set ; This makes sure 4 bits ( From MSB and including MSB ) are set ; Driver code
function setAllBitsAfterMSB ( n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return n ; } function toggle ( n ) { n = n ^ setAllBitsAfterMSB ( n ) ; return n ; } let n = 10 ; document . write ( toggle ( n ) ) ;
Check if a number has two adjacent set bits | Javascript program to check if there are two adjacent set bits . ; driver program
function adjacentSet ( n ) { let x = ( n & ( n >> 1 ) ) ; if ( x > 0 ) return true ; else return false ; } let n = 3 ; if ( adjacentSet ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Position of rightmost common bit in two numbers | Function to find the position of rightmost set bit in ' n ' ; Function to find the position of rightmost same bit in the binary representations of ' m ' and ' n ' ; position of rightmost same bit ; Driver program to test above
function getRightMostSetBit ( n ) { return Math . log2 ( n & - n ) + 1 ; } function posOfRightMostSameBit ( m , n ) { return getRightMostSetBit ( ~ ( m ^ n ) ) ; } let m = 16 , n = 7 ; document . write ( " " + posOfRightMostSameBit ( m , n ) ) ;
Position of rightmost common bit in two numbers | Function to find the position of rightmost same bit in the binary representations of ' m ' and ' n ' ; Initialize loop counter ; Check whether the value ' m ' is odd ; Check whether the value ' n ' is odd ; Below ' if ' checks for both values to be odd or even ; Right shift value of m ; Right shift value of n ; When no common set is found ; Driver code
function posOfRightMostSameBit ( m , n ) { let loopCounter = 1 ; while ( m > 0 n > 0 ) { let a = m % 2 == 1 ; let b = n % 2 == 1 ; if ( ! ( a ^ b ) ) { return loopCounter ; } m = m >> 1 ; n = n >> 1 ; loopCounter ++ ; } return - 1 ; } let m = 16 , n = 7 ; document . write ( " " + posOfRightMostSameBit ( m , n ) ) ;
Check whether all the bits are set in the given range | 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 ; 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 " " ; return " " ; } var n = 22 ; var l = 2 , r = 3 ; document . write ( allBitsSetInTheGivenRange ( n , l , r ) ) ;
1 to n bit numbers with no consecutive 1 s in binary representation | Javascript Code to Print all numbers upto n bits with no consecutive set bits . ; Let us first compute 2 raised to power n . ; loop 1 to n to check all the numbers ; A number i doesn ' t ▁ contain ▁ ▁ consecutive ▁ set ▁ bits ▁ if ▁ ▁ bitwise ▁ and ▁ of ▁ i ▁ and ▁ left ▁ ▁ shifted ▁ i ▁ do ' t contain a commons set bit . ; driver program
function printNonConsecutive ( n ) { let p = ( 1 << n ) ; for ( let i = 1 ; i < p ; i ++ ) if ( ( i & ( i << 1 ) ) == 0 ) document . write ( i + " " ) ; } let n = 3 ; printNonConsecutive ( n ) ;
Find the n | Efficient javascript program to find n - th palindrome ; Construct the nth binary palindrome with the given group number , aux_number and operation type ; No need to insert any bit in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and  fill around middle ; Insert bit 0 in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and  fill around middle ; Insert bit 1 in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and  fill around middle ; Convert the number to decimal from binary ; Will return the nth binary palindrome number ; Add number of elements in all the groups , until the group of the nth number is found ; Total number of elements until this group ; Element 's offset position in the group ; Finding which bit to be placed in the middle and finding the number , which we will fill from the middle in both directions ; No need to put extra bit in middle ; We need to fill this auxiliary number in binary form the middle in both directions ; Need to Insert 0 at middle ; Need to Insert 1 at middle ; Driver code ; Function Call
var INT_SIZE = 32 ; function constructNthNumber ( group_no , aux_num , op ) { var a = Array . from ( { length : INT_SIZE } , ( _ , i ) => 0 ) ; var num = 0 , len_f = 0 ; var i = 0 ; if ( op == 2 ) { len_f = 2 * group_no ; a [ len_f - 1 ] = a [ 0 ] = 1 ; while ( aux_num > 0 ) { a [ group_no + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } else if ( op == 0 ) { len_f = 2 * group_no + 1 ; a [ len_f - 1 ] = a [ 0 ] = 1 ; a [ group_no ] = 0 ; while ( aux_num > 0 ) { a [ group_no + 1 + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } else { len_f = 2 * group_no + 1 ; a [ len_f - 1 ] = a [ 0 ] = 1 ; a [ group_no ] = 1 ; while ( aux_num > 0 ) { a [ group_no + 1 + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } for ( i = 0 ; i < len_f ; i ++ ) num += ( 1 << i ) * a [ i ] ; return num ; } function getNthNumber ( n ) { var group_no = 0 , group_offset ; var count_upto_group = 0 , count_temp = 1 ; var op , aux_num ; while ( count_temp < n ) { group_no ++ ; count_upto_group = count_temp ; count_temp += 3 * ( 1 << ( group_no - 1 ) ) ; } group_offset = n - count_upto_group - 1 ; if ( ( group_offset + 1 ) <= ( 1 << ( group_no - 1 ) ) ) { op = 2 ; aux_num = group_offset ; } else { if ( ( ( group_offset + 1 ) - ( 1 << ( group_no - 1 ) ) ) % 2 == 1 ) op = 0 ; else op = 1 ; aux_num = ( ( group_offset ) - ( 1 << ( group_no - 1 ) ) ) / 2 ; } return constructNthNumber ( group_no , aux_num , op ) ; } var n = 9 ; document . write ( getNthNumber ( n ) ) ;
Shuffle a pack of cards and answer the query | function to find card at given index ; Answer will be reversal of N bits from MSB ; Calculating the reverse binary representation ; Printing the result ; No . of Shuffle Steps ; Key position
function shuffle ( N , key ) { let NO_OF_BITS = N ; let reverse_num = 0 , temp ; for ( let i = 0 ; i < NO_OF_BITS ; i ++ ) { temp = ( key & ( 1 << i ) ) ; if ( temp > 0 ) reverse_num |= ( 1 << ( ( NO_OF_BITS - 1 ) - i ) ) ; } document . write ( reverse_num ) ; } let N = 3 ; let key = 3 ; shuffle ( N , key ) ;
Sum of numbers with exactly 2 bits set | To count number of set bits ; To calculate sum of numbers ; To count sum of number whose 2 bit are set ; Driver program to test above function
function countSetBits ( n ) { let count = 0 ; while ( n ) { n &= ( n - 1 ) ; count ++ ; } return count ; } function findSum ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) if ( countSetBits ( i ) == 2 ) sum += i ; return sum ; } let n = 10 ; document . write ( findSum ( n ) ) ;
Toggle the last m bits | function to toggle the last m bits ; calculating a number ' num ' having ' m ' bits and all are set . ; toggle the last m bits and return the number ; Driver code
function toggleLastMBits ( n , m ) { var num = ( 1 << m ) - 1 ; return ( n ^ num ) ; } var n = 107 ; var m = 4 ; document . write ( toggleLastMBits ( n , m ) ) ;
Set the rightmost unset bit | function to find the position of rightmost set bit ; if n = 0 , return 1 ; if all bits of ' n ' are set ; position of rightmost unset bit in ' n ' passing ~ n as argument ; set the bit at position ' pos ' ; Driver code
function getPosOfRightmostSetBit ( n ) { return ( ( Math . log10 ( n & - n ) ) / ( Math . log10 ( 2 ) ) ) + 1 ; } function setRightmostUnsetBit ( n ) { if ( n == 0 ) return 1 ; if ( ( n & ( n + 1 ) ) == 0 ) return n ; let pos = getPosOfRightmostSetBit ( ~ n ) ; return ( ( 1 << ( pos - 1 ) ) n ) ; } let n = 21 ; document . write ( setRightmostUnsetBit ( n ) ) ;
Previous smaller integer having one less number of set bits | function to find the position of rightmost set bit . ; function to find the previous smaller integer ; position of rightmost set bit of n ; turn off or unset the bit at position ' pos ' ; Driver program
function getFirstSetBitPos ( n ) { return parseInt ( Math . log ( n & - n ) / Math . log ( 2 ) ) + 1 ; } function previousSmallerInteger ( n ) { var pos = getFirstSetBitPos ( n ) ; return ( n & ~ ( 1 << ( pos - 1 ) ) ) ; } var n = 25 ; document . write ( " " + previousSmallerInteger ( n ) ) ;
Convert a binary number to octal | Function to create map between binary number and its equivalent hexadecimal ; Function to find octal equivalent of binary ; Length of string before ' . ' ; Add min 0 's in the beginning to make  left substring length divisible by 3 ; If decimal point exists ; Length of string after ' . ' ; add min 0 's in the end to make right  substring length divisible by 3 ; Create map between binary and its equivalent octal code ; One by one extract from left , substring of size 3 and add its octal code ; If ' . ' is encountered add it to result ; Required octal number ; Driver code
function createMap ( um ) { um . set ( " " , ' ' ) ; um . set ( " " , ' ' ) ; um . set ( " " , ' ' ) ; um . set ( " " , ' ' ) ; um . set ( " " , ' ' ) ; um . set ( " " , ' ' ) ; um . set ( " " , ' ' ) ; um . set ( " " , ' ' ) ; } function convertBinToOct ( bin ) { let l = bin . length ; let t = bin . indexOf ( ' ' ) ; let len_left = t != - 1 ? t : l ; for ( let i = 1 ; i <= ( 3 - len_left % 3 ) % 3 ; i ++ ) bin = ' ' + bin ; if ( t != - 1 ) { let len_right = l - len_left - 1 ; for ( let i = 1 ; i <= ( 3 - len_right % 3 ) % 3 ; i ++ ) bin = bin + ' ' ; } let bin_oct_map = new Map ( ) ; createMap ( bin_oct_map ) ; let i = 0 ; let octal = " " ; while ( true ) { octal += bin_oct_map . get ( bin . substr ( i , 3 ) ) ; i += 3 ; if ( i == bin . length ) break ; if ( bin . charAt ( i ) == ' ' ) { octal += ' ' ; i ++ ; } } return octal ; } let bin = " " ; document . write ( " " + convertBinToOct ( bin ) ) ;
Check if all bits of a number are set | function to check if all the bits are setthe bits are set or not in the binary representation of ' n ' ; all bits are not set ; loop till n becomes '0' ; if the last bit is not set ; right shift ' n ' by 1 ; all bits are set ; Driver program to test above
function areAllBitsSet ( n ) { if ( n == 0 ) return " " ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) return " " ; n = n >> 1 ; } return " " ; } var n = 7 ; document . write ( areAllBitsSet ( n ) ) ;
Check if all bits of a number are set | function to check if all the bits are set or not in the binary representation of ' n ' ; all bits are not set ; if true , then all bits are set ; else all bits are not set ; Driver program to test above
function areAllBitsSet ( n ) { if ( n == 0 ) return " " ; if ( ( ( n + 1 ) & n ) == 0 ) return " " ; return " " ; } var n = 7 ; document . write ( areAllBitsSet ( n ) ) ;
Next greater integer having one more number of set bits | function to find the position of rightmost set bit . Returns - 1 if there are no set bits ; function to find the next greater integer ; position of rightmost unset bit of n by passing ~ n as argument ; if n consists of unset bits , then set the rightmost unset bit ; n does not consists of unset bits ; Driver program to test above
function getFirstSetBitPos ( n ) { return ( parseInt ( Math . log ( n & - n ) / Math . log ( 2 ) ) + 1 ) - 1 ; } function nextGreaterWithOneMoreSetBit ( n ) { var pos = getFirstSetBitPos ( ~ n ) ; if ( pos > - 1 ) return ( 1 << pos ) | n ; return ( ( n << 1 ) + 1 ) ; } var n = 10 ; document . write ( " " + nextGreaterWithOneMoreSetBit ( n ) ) ;
Toggle all the bits of a number except k | Returns a number with all bit toggled in n except k - th bit ; 1 ) Toggle k - th bit by doing n ^ ( 1 << k ) 2 ) Toggle all bits of the modified number ; Driver code
function toggleAllExceptK ( n , k ) { return ~ ( n ^ ( 1 << k ) ) ; } let n = 4294967295 ; let k = 0 ; document . write ( toggleAllExceptK ( n , k ) ) ;
Count numbers whose sum with x is equal to XOR with x | Function to find total 0 bit in a number ; Function to find Count of non - negative numbers less than or equal to x , whose bitwise XOR and SUM with x are equal . ; count number of zero bit in x ; power of 2 to count ; Driver code ; Function call
function CountZeroBit ( x ) { let count = 0 ; while ( x > 0 ) { if ( ( x & 1 ) == 0 ) count ++ ; x >>= 1 ; } return count ; } function CountXORandSumEqual ( x ) { let count = CountZeroBit ( x ) ; return ( 1 << count ) ; } let x = 10 ; document . write ( CountXORandSumEqual ( x ) ) ;
Find missing number in another array which is shuffled copy | Returns the missing number Size of arr2 [ ] is n - 1 ; Missing number ' mnum ' ; 1 st array is of size ' n ' ; 2 nd array is of size ' n ▁ - ▁ 1' ; Required missing number ; Driver Code
function missingNumber ( arr1 , arr2 , n ) { let mnum = 0 ; for ( let i = 0 ; i < n ; i ++ ) mnum = mnum ^ arr1 [ i ] ; for ( let i = 0 ; i < n - 1 ; i ++ ) mnum = mnum ^ arr2 [ i ] ; return mnum ; } let arr1 = [ 4 , 8 , 1 , 3 , 7 ] ; let arr2 = [ 7 , 4 , 3 , 1 ] ; let n = arr1 . length ; document . write ( " " + missingNumber ( arr1 , arr2 , n ) ) ;
Count smaller values whose XOR with x is greater than x | Javascript program to find count of values whose XOR with x is greater than x and values are smaller than x ; Initialize result ; Traversing through all bits of x ; If current last bit of x is set then increment count by n . Here n is a power of 2 corresponding to position of bit ; Simultaneously calculate the 2 ^ n ; Replace x with x / 2 ; ; Driver code
function countValues ( x ) { var count = 0 , n = 1 ; while ( x != 0 ) { if ( x % 2 == 0 ) count += n ; n *= 2 ; x = parseInt ( x / 2 ) ; } return count ; } var x = 10 ; document . write ( countValues ( x ) ) ;
Construct an array from XOR of all elements of array except element at same index | function to construct new array ; calculate xor of array ; update array ; Driver code ; print result
function constructXOR ( A , n ) { let XOR = 0 ; for ( let i = 0 ; i < n ; i ++ ) XOR ^= A [ i ] ; for ( let i = 0 ; i < n ; i ++ ) A [ i ] = XOR ^ A [ i ] ; } let A = [ 2 , 4 , 1 , 3 , 5 ] ; let n = A . length ; constructXOR ( A , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( A [ i ] + " " ) ;
Count all pairs of an array which differ in K bits | Utility function to count total ones in a number ; Function to count pairs of K different bits ; let ans = 0 ; initialize final answer ; Check for K differ bit ; Driver Code
function bitCount ( n ) { let count = 0 ; while ( n > 0 ) { if ( ( n & 1 ) > 0 ) ++ count ; n >>= 1 ; } return count ; } function countPairsWithKDiff ( arr , n , k ) { for ( let i = 0 ; i < n - 1 ; ++ i ) { for ( let j = i + 1 ; j < n ; ++ j ) { let xoredNum = arr [ i ] ^ arr [ j ] ; if ( k == bitCount ( xoredNum ) ) ++ ans ; } } return ans ; } let k = 2 ; let arr = [ 2 , 4 , 1 , 3 , 1 ] ; let n = arr . length ; document . write ( " " + k + " " + countPairsWithKDiff ( arr , n , k ) + " " ) ;
Count all pairs of an array which differ in K bits | Function to calculate K bit different pairs in array ; Get the maximum value among all array elemensts ; Set the count array to 0 , count [ ] stores the total frequency of array elements ; Initialize result ; For 0 bit answer will be total count of same number ; if count [ i ] is 0 , skip the next loop as it will not contribute the answer ; Update answer if k differ bit found ; Driver code
function kBitDifferencePairs ( arr , n , k ) { let MAX = Math . max ( ... arr ) ; let count = new Array ( MAX + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; ++ i ) ++ count [ arr [ i ] ] ; let ans = 0 ; if ( k == 0 ) { for ( let i = 0 ; i <= MAX ; ++ i ) ans += parseInt ( ( count [ i ] * ( count [ i ] - 1 ) ) / 2 ) ; return ans ; } for ( let i = 0 ; i <= MAX ; ++ i ) { if ( ! count [ i ] ) continue ; for ( let j = i + 1 ; j <= MAX ; ++ j ) { if ( BitCount ( i ^ j ) == k ) ans += count [ i ] * count [ j ] ; } } return ans ; } function BitCount ( n ) { let count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } let k = 2 ; let arr = [ 2 , 4 , 1 , 3 , 1 ] ; let n = arr . length ; document . write ( " " + k + " " + kBitDifferencePairs ( arr , n , k ) + " " ) ; k = 3 ; document . write ( " " + k + " " + kBitDifferencePairs ( arr , n , k ) + " " ) ;
Ways to represent a number as a sum of 1 ' s ▁ and ▁ 2' s | Function to multiply matrix . ; Power function in log n ; function that returns ( n + 1 ) th Fibonacci number Or number of ways to represent n as sum of 1 ' s ▁ 2' s ; Driver program
function multiply ( F , M ) { var x = F [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] ; var y = F [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] + F [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] ; var z = F [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] + F [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] ; var w = F [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] + F [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] ; F [ 0 ] [ 0 ] = x ; F [ 0 ] [ 1 ] = y ; F [ 1 ] [ 0 ] = z ; F [ 1 ] [ 1 ] = w ; } function power ( F , n ) { if ( n == 0 n == 1 ) { return ; } var M = [ [ 1 , 1 ] , [ 1 , 0 ] ] ; power ( F , parseInt ( n / 2 ) ) ; multiply ( F , F ) ; if ( n % 2 != 0 ) { multiply ( F , M ) ; } } function countWays ( n ) { var F = [ [ 1 , 1 ] , [ 1 , 0 ] ] ; if ( n == 0 ) { return 0 ; } power ( F , n ) ; return F [ 0 ] [ 0 ] ; } var n = 5 ; document . write ( countWays ( n ) ) ;
Multiplication of two numbers with shift operator | Function for multiplication ; check for set bit and left shift n , count times ; increment of place value ( count ) ; Driver code
function multiply ( n , m ) { let ans = 0 , count = 0 ; while ( m ) { if ( m % 2 == 1 ) ans += n << count ; count ++ ; m = Math . floor ( m / 2 ) ; } return ans ; } let n = 20 , m = 13 ; document . write ( multiply ( n , m ) ) ;
Compare two integers without using any Comparison operator | function return true if A ^ B > 0 else false ; Driver program
function EqualNumber ( A , B ) { return ( A ^ B ) ; } let A = 5 , B = 6 ; if ( ! EqualNumber ( A , B ) == false ) document . write ( " " ) ; else document . write ( " " ) ;
Check if bits of a number has count of consecutive set bits in increasing order | JavaScript program to find if bit - pattern of a number has increasing value of continuous - 1 or not . ; Returns true if n has increasing count of continuous - 1 else false ; store the bit - pattern of n into bit bitset - bp ; set prev_count = 0 and curr_count = 0. ; increment current count of continuous - 1 ; traverse all continuous - 0 ; check prev_count and curr_count on encounter of first zero after continuous - 1 s ; check for last sequence of continuous - 1 ; Driver code
function dec2bin ( dec ) { return ( dec >>> 0 ) . toString ( 2 ) ; } function findContinuous1 ( n ) { let bp = dec2bin ( n ) let bits = bp . length let prev_count = 0 let curr_count = 0 let i = 0 while ( i < bits ) { if ( bp [ i ] == ' ' ) { curr_count += 1 ; i += 1 ; } else if ( bp [ i - 1 ] == ' ' ) { i += 1 curr_count = 0 continue } else { if ( curr_count < prev_count ) return 0 i += 1 prev_count = curr_count curr_count = 0 } } if ( prev_count > curr_count && ( curr_count != 0 ) ) return 0 return 1 } n = 179 if ( findContinuous1 ( n ) ) document . write ( " " ) else document . write ( " " )
Check if bits of a number has count of consecutive set bits in increasing order | Returns true if n has counts of consecutive 1 's are increasing order. ; Initialize previous count ; We traverse bits from right to left and check if counts are decreasing order . ; Ignore 0 s until we reach a set bit . ; Count current set bits ; Compare current with previous and update previous . ; Driver code
function areSetBitsIncreasing ( n ) { var prev_count = Number . MAX_VALUE ; while ( n > 0 ) { while ( n > 0 && n % 2 == 0 ) n = parseInt ( n / 2 ) ; var curr_count = 1 ; while ( n > 0 && n % 2 == 1 ) { n = n / 2 ; curr_count ++ ; } if ( curr_count >= prev_count ) return false ; prev_count = curr_count ; } return true ; } var n = 10 ; if ( areSetBitsIncreasing ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a number has bits in alternate pattern | Set 1 | Returns true if n has alternate bit pattern else false ; Store last bit ; Traverse through remaining bits ; If current bit is same as previous ; Driver code
function findPattern ( n ) { let prev = n % 2 ; n = Math . floor ( n / 2 ) ; while ( n > 0 ) { let curr = n % 2 ; if ( curr == prev ) return false ; prev = curr ; n = Math . floor ( n / 2 ) ; } return true ; } let n = 10 ; if ( findPattern ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
XOR counts of 0 s and 1 s in binary representation | Returns XOR of counts 0 s and 1 s in binary representation of n . ; calculating count of zeros and ones ; Driver Program
function countXOR ( n ) { let count0 = 0 , count1 = 0 ; while ( n != 0 ) { if ( n % 2 == 0 ) count0 ++ ; else count1 ++ ; n = Math . floor ( n / 2 ) ; } return ( count0 ^ count1 ) ; } let n = 31 ; document . write ( countXOR ( n ) ) ;
Count all pairs with given XOR | Returns count of pairs in arr [ 0. . n - 1 ] with XOR value equals to x . ; create empty map that stores counts of individual elements of array . ; If there exist an element in map m with XOR equals to x ^ arr [ i ] , that means there exist an element such that the XOR of element with arr [ i ] is equal to x , then increment count . ; Increment count of current element ; return total count of pairs with XOR equal to x ; Driver program
function xorPairCount ( arr , n , x ) { let m = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let curr_xor = x ^ arr [ i ] ; if ( m . has ( curr_xor ) ) result += m . get ( curr_xor ) ; if ( m . has ( arr [ i ] ) ) { m . set ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . set ( arr [ i ] , 1 ) ; } } return result ; } let arr = [ 2 , 5 , 2 ] ; let n = arr . length ; let x = 0 ; document . write ( " " + xorPairCount ( arr , n , x ) ) ;
Bitwise and ( or & ) of a range | Find position of MSB in n . For example if n = 17 , then position of MSB is 4. If n = 7 , value of MSB is 3 ; Function to find Bit - wise & of all numbers from x to y . ; let res = 0 ; Initialize result ; Find positions of MSB in x and y ; If positions are not same , return ; Add 2 ^ msb_p1 to result ; subtract 2 ^ msb_p1 from x and y . ; Driver Code
function msbPos ( n ) { let msb_p = - 1 ; while ( n > 0 ) { n = n >> 1 ; msb_p ++ ; } return msb_p ; } function andOperator ( x , y ) { while ( x > 0 && y > 0 ) { let msb_p1 = msbPos ( x ) ; let msb_p2 = msbPos ( y ) ; if ( msb_p1 != msb_p2 ) break ; let msb_val = ( 1 << msb_p1 ) ; res = res + msb_val ; x = x - msb_val ; y = y - msb_val ; } return res ; } let x = 10 , y = 15 ; document . write ( andOperator ( x , y ) ) ;
Multiply a number with 10 without using multiplication operator | Function to find multiplication of n with 10 without using multiplication operator ; Driver program to run the case
function multiplyTen ( n ) { return ( n << 1 ) + ( n << 3 ) ; } let n = 50 ; document . write ( multiplyTen ( n ) ) ;