codeparrot/xlcost-text-to-code huggingface dataset:
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 . ; count the step number ; if one element is there , it will be the answer ; at first step we do a bitwise OR ; keep on doing bitwise operations till the last element is left ; perform operations ; if step is the odd step ; else even step ; answer when one element is left ; Driver Code ; 1 st query ; 2 nd query
var N = 1000 function lastElement ( a , n ) { var steps = 1 ; var v = Array . from ( Array ( N ) , ( ) => Array ( 0 ) ) ; if ( n == 1 ) return a [ 0 ] ; for ( var i = 0 ; i < n ; i += 2 ) v [ steps ] . push ( a [ i ] a [ i + 1 ] ) ; while ( v [ steps ] . length > 1 ) { steps += 1 ; for ( var i = 0 ; i < v [ steps - 1 ] . length ; i += 2 ) { if ( steps & 1 ) v [ steps ] . push ( v [ steps - 1 ] [ i ] v [ steps - 1 ] [ i + 1 ] ) ; v [ steps ] . push ( v [ steps - 1 ] [ i ] ^ v [ steps - 1 ] [ i + 1 ] ) ; } } return v [ steps ] [ 0 ] ; } var a = [ 1 , 4 , 5 , 6 ] ; var n = a . length ; var index = 0 ; var value = 2 ; a [ 0 ] = 2 ; document . write ( lastElement ( a , n ) + " " ) ; index = 3 ; value = 5 ; a [ index ] = value ; document . write ( lastElement ( a , n ) ) ;
Find the winner in nim | function to find winner of NIM - game ; case when Alice is winner ; when Bob is winner ; Driver Code
function findWinner ( A , n ) { let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) res ^= A [ i ] ; if ( res == 0 n % 2 == 0 ) return " " ; else return " " ; } let A = [ 1 , 4 , 3 , 5 ] ; let n = A . length ; document . write ( " " + findWinner ( A , n ) ) ;
Fibbinary Numbers ( No consecutive 1 s in binary ) | function to check whether a number is fibbinary or not ; if the number does not contain adjacent ones then ( n & ( n >> 1 ) ) operation results to 0 ; not a fibbinary number ; Driver code
function isFibbinaryNum ( n ) { if ( ( n & ( n >> 1 ) ) == 0 ) return true ; return false ; } let n = 10 ; if ( isFibbinaryNum ( n ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Maximum XOR | function to calculate maximum XOR value ; Return ( 2 ^ c - 1 ) ; Driver code
function maxXOR ( n , k ) { let c = ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ; return ( ( 1 << c ) - 1 ) ; } let n = 12 ; let k = 3 ; document . write ( maxXOR ( n , k ) ) ;
Divide two integers without using multiplication , division and mod operator | Function to divide a by b and return floor value it ; Calculate sign of divisor i . e . , sign will be negative only iff either one of them is negative otherwise it will be positive ; remove sign of operands ; Initialize the quotient ; test down from the highest bit and accumulate the tentative value for valid bit ; if the sign value computed earlier is - 1 then negate the value of quotient ; Driver code
function divide ( dividend , divisor ) { var sign = ( ( dividend < 0 ) ? 1 : 0 ^ ( divisor < 0 ) ? 1 : 0 ) ? - 1 : 1 ; dividend = Math . abs ( dividend ) ; divisor = Math . abs ( divisor ) ; var quotient = 0 , temp = 0 ; while ( dividend >= divisor ) { dividend -= divisor ; ++ quotient ; } if ( sign == - 1 ) quotient = - quotient ; return quotient ; } var a = 10 , b = 3 ; document . write ( divide ( a , b ) + " " ) ; var a1 = 43 , b1 = - 8 ; document . write ( divide ( a1 , b1 ) + " " ) ;
XOR of two numbers after making length of their binary representations equal | function to count the number of bits in binary representation of an integer ; initialize count ; count till n is non zero ; right shift by 1 i . e , divide by 2 ; function to calculate the xor of two numbers by adding trailing zeros to the number having less number of bits in its binary representation . ; stores the minimum and maximum ; left shift if the number of bits are less in binary representation ; Driver code
function count ( n ) { let c = 0 ; while ( n != 0 ) { c ++ ; n = n >> 1 ; } return c ; } function XOR ( a , b ) { let c = Math . min ( a , b ) ; let d = Math . max ( a , b ) ; if ( count ( c ) < count ( d ) ) c = c << ( count ( d ) - count ( c ) ) ; return ( c ^ d ) ; } let a = 13 , b = 5 ; document . write ( XOR ( a , b ) ) ;
Check if binary string multiple of 3 using DFA | Function to check if the binary String is divisible by 3. ; checking if the bit is nonzero ; checking if the nonzero bit is at even position ; Checking if the difference of non - zero oddbits and evenbits is divisible by 3. ; Driver Program
function CheckDivisibilty ( A ) { var oddbits = 0 , evenbits = 0 ; for ( var counter = 0 ; counter < A . length ; counter ++ ) { if ( A [ counter ] == ' ' ) { if ( counter % 2 == 0 ) { evenbits ++ ; } else { oddbits ++ ; } } } if ( Math . abs ( oddbits - evenbits ) % 3 == 0 ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; } } var A = " " ; CheckDivisibilty ( A ) ;
Swap every two bits in bytes | java script program to swap every two bits in a byte . ; Extracting the high bit shift it to lowbit Extracting the low bit shift it to highbit ; Driver Code
function swapBitsInPair ( x ) { return ( ( x & 0b10101010 ) >> 1 ) | ( ( x & 0b01010101 ) << 1 ) ; } let x = 4 ; document . write ( swapBitsInPair ( x ) ) ;
Alternate bits of two numbers to create a new number | set even bit of number n ; res for store 101010. . number ; generate number form of 101010. . ... till temp size ; if bit is even then generate number and or with res ; return set even bit number ; set odd bit of number m ; res for store 101010. . number ; generate number form of 101010. . . . till temp size ; if bit is even then generate number and or with res ; return set odd bit number ; set even bit of number n ; set odd bit of number m ; take OR with these number ; Driver code ; n = 1 0 1 0 ^ ^ m = 1 0 1 1 ^ ^ result = 1 0 1 1
function setevenbits ( n ) { var temp = n ; var count = 0 ; var res = 0 ; for ( temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 1 ) res |= ( 1 << count ) ; count ++ ; } return ( n & res ) ; } function setoddbits ( m ) { var count = 0 ; var res = 0 ; for ( temp = m ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 0 ) res |= ( 1 << count ) ; count ++ ; } return ( m & res ) ; } function getAlternateBits ( n , m ) { var tempn = setevenbits ( n ) ; var tempm = setoddbits ( m ) ; return ( tempn tempm ) ; } var n = 10 ; var m = 11 ; document . write ( getAlternateBits ( n , m ) ) ;
Decimal representation of given binary string is divisible by 20 or not | function to check whether decimal representation of given binary number is divisible by 10 or not ; if last digit is '1' , then number is not divisible by 10 ; to accumulate the sum of last digits in perfect powers of 2 ; traverse from the 2 nd last up to 1 st digit in ' bin ' ; if digit in '1' ; calculate digit 's position from  the right ; according to the digit 's position,  obtain the last digit of the  applicable perfect power of 2 ; if last digit is 0 , then divisible by 10 ; not divisible by 10 ; function to check whether decimal representation of given binary number is divisible by 20 or not ; if ' bin ' is an odd number ; check if bin ( 0. . n - 2 ) is divisible by 10 or not ; Driver program to test above
function isDivisibleBy10 ( bin , n ) { if ( bin [ n - 1 ] == ' ' ) return false ; var sum = 0 ; for ( var i = n - 2 ; i >= 0 ; i -- ) { if ( bin [ i ] == ' ' ) { var posFromRight = n - i - 1 ; if ( posFromRight % 4 == 1 ) sum = sum + 2 ; else if ( posFromRight % 4 == 2 ) sum = sum + 4 ; else if ( posFromRight % 4 == 3 ) sum = sum + 8 ; else if ( posFromRight % 4 == 0 ) sum = sum + 6 ; } } if ( sum % 10 == 0 ) return true ; return false ; } function isDivisibleBy20 ( bin , n ) { if ( bin [ n - 1 ] == ' ' ) return false ; return isDivisibleBy10 ( bin , n - 1 ) ; } var bin = " " ; var n = bin . length ; if ( isDivisibleBy20 ( bin , n - 1 ) ) document . write ( " " ) ; else document . write ( " " ) ;
For every set bit of a number toggle bits of other | Function for the Nega_bit ; Driver code
function toggleBits ( n1 , n2 ) { return ( n1 ^ n2 ) ; } let n1 = 2 , n2 = 5 ; document . write ( toggleBits ( n1 , n2 ) ) ;
Toggle all even bits of a number | Returns a number which has all even bits of n toggled . ; Generate number form of 101010 . . till of same order as n ; if bit is even then generate number and or with res ; return toggled number ; Driver code
function evenbittogglenumber ( n ) { let res = 0 , count = 0 ; for ( let temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 1 ) res |= ( 1 << count ) ; count ++ ; } return n ^ res ; } let n = 11 ; document . write ( evenbittogglenumber ( n ) ) ;
Toggle first and last bits of a number | Returns a number which has same bit count as n and has only first and last bits as set . ; set all the bit of the number ; Adding one to n now unsets all bits and moves MSB to one place . Now we shift the number by 1 and add 1. ; if number is 1 ; take XOR with first and last set bit number ; Driver code
function takeLandFsetbits ( n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( ( n + 1 ) >> 1 ) + 1 ; } function toggleFandLbits ( n ) { if ( n == 1 ) return 0 ; return n ^ takeLandFsetbits ( n ) ; } let n = 10 ; document . write ( toggleFandLbits ( n ) ) ;
Odious number | Function to get no of set bits in binary representation of passed binary no . Please refer below for details of this function : https : www . geeksforgeeks . org / count - set - bits - in - an - integer / ; Check if number is odious or not ; Driver code
function countSetBits ( n ) { let count = 0 ; while ( n != 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } function checkOdious ( n ) { return ( countSetBits ( n ) % 2 == 1 ) ; } let num = 32 ; if ( checkOdious ( num ) ) document . write ( " " ) ; else document . write ( " " ) ;
Set the Left most unset bit | Set left most unset bit ; If number contain all 1 then return n ; Find position of leftmost unset bit . ; If temp L . S . B is zero then unset bit pos is change ; Return OR of number and unset bit pos ; Driver Code
function setleftmostunsetbit ( n ) { if ( ( n & ( n + 1 ) ) == 0 ) return n ; let pos = 0 ; for ( let temp = n , count = 0 ; temp > 0 ; temp >>= 1 , count ++ ) if ( ( temp & 1 ) == 0 ) pos = count ; return ( n | ( 1 << ( pos ) ) ) ; } let n = 10 ; document . write ( setleftmostunsetbit ( n ) ) ;
Maximum XOR using K numbers from 1 to n | To return max xor sum of 1 to n using at most k numbers ; If k is 1 then maximum possible sum is n ; Finding number greater than or equal to n with most significant bit same as n . For example , if n is 4 , result is 7. If n is 5 or 6 , result is 7 ; Return res - 1 which denotes a number with all bits set to 1 ; Driver code
function maxXorSum ( n , k ) { if ( k == 1 ) return n ; let res = 1 ; while ( res <= n ) res <<= 1 ; return res - 1 ; } let n = 4 , k = 3 ; document . write ( maxXorSum ( n , k ) ) ;
Increment a number by one by manipulating the bits | function to find the position of rightmost set bit ; 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 ; function to increment a number by one by manipulating the bits ; get position of rightmost unset bit if all bits of ' n ' are set , then the bit left to the MSB is the rightmost unset bit ; kth bit of n is being set by this operation ; from the right toggle all the bits before the k - th bit ; required number ; Driver code
function getPosOfRightmostSetBit ( n ) { return ( Math . log ( n & - n ) / Math . log ( 2 ) ) ; } function toggleLastKBits ( n , k ) { let num = ( 1 << k ) - 1 ; return ( n ^ num ) ; } function incrementByOne ( n ) { let k = getPosOfRightmostSetBit ( ~ n ) ; n = ( ( 1 << k ) n ) ; if ( k != 0 ) n = toggleLastKBits ( n , k ) ; return n ; } let n = 15 ; document . write ( incrementByOne ( n ) ) ;
XNOR of two numbers | javascript program to find XNOR of two numbers ; Make sure a is larger ; swapping a and b ; ; for last bit of a ; for last bit of b ; counter for count bit and set bit in xnornum ; to make new xnor number ; for set bits in new xnor number ; get last bit of a ; get last bit of b ; Check if current two bits are same ; counter for count bit ; Driver Function
function xnor ( a , b ) { if ( a < b ) { let t = a ; a = b ; b = t ; } if ( a == 0 && b == 0 ) return 1 ; let a_rem = 0 ; let b_rem = 0 ; let count = 0 ; let xnornum = 0 ; while ( true ) { a_rem = a & 1 ; b_rem = b & 1 ; if ( a_rem == b_rem ) xnornum |= ( 1 << count ) ; count ++ ; a = a >> 1 ; b = b >> 1 ; if ( a < 1 ) break ; } return xnornum ; } let a = 10 , b = 50 ; document . write ( xnor ( a , b ) ) ;
XNOR of two numbers | Please refer below post for details of this function https : www . geeksforgeeks . org / toggle - bits - significant - bit / ; Make a copy of n as we are going to change it . ; Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001 ; This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111 ; Returns XNOR of num1 and num2 ; if num2 is greater then we swap this number in num1 ; Driver code
function togglebit ( n ) { if ( n == 0 ) return 1 ; let i = n ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return i ^ n ; } function XNOR ( num1 , num2 ) { if ( num1 < num2 ) { let temp = num1 ; num1 = num2 ; num2 = temp ; } num1 = togglebit ( num1 ) ; return num1 ^ num2 ; } let num1 = 10 , num2 = 20 ; document . write ( XNOR ( num1 , num2 ) ) ;
Maximum OR sum of sub | function to find maximum OR sum ; OR sum of all the elements in both arrays ; Driver code
function MaximumSum ( a , b , n ) { let sum1 = 0 , sum2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum1 |= a [ i ] ; sum2 |= b [ i ] ; } document . write ( sum1 + sum2 ) ; } let A = [ 1 , 2 , 4 , 3 , 2 ] ; let B = [ 2 , 3 , 3 , 12 , 1 ] ; let n = A . length ; MaximumSum ( A , B , n ) ;
Position of rightmost bit with first carry in sum of two binary | Function to find the position of rightmost set bit in ' n ' ; Function to find the position of rightmost bit where a carry is generated first ; Driver code
function posOfRightmostSetBit ( n ) { return parseInt ( Math . log ( n & - n ) / Math . log ( 2 ) ) + 1 ; } function posOfCarryBit ( a , b ) { return posOfRightmostSetBit ( a & b ) ; } var a = 10 , b = 2 ; document . write ( posOfCarryBit ( a , b ) ) ;
Check whether the two numbers differ at one bit position only | function to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; function to check whether the two numbers differ at one bit position only ; Driver code
function isPowerOfTwo ( x ) { return x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ; } function differAtOneBitPos ( a , b ) { return isPowerOfTwo ( a ^ b ) ; } let a = 13 , b = 9 ; if ( differAtOneBitPos ( a , b ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Multiplication with a power of 2 | Returns 2 raised to power n ; Driver Code
function power2 ( n ) { if ( n == 0 ) return 1 ; if ( n == 1 ) return 2 ; return power2 ( n / 2 ) * power2 ( n / 2 ) ; } function multiply ( x , n ) { return x * power2 ( n ) ; } let x = 70 let n = 2 ; document . write ( multiply ( x , n ) ) ;
Multiplication with a power of 2 | Efficient JavaScript program to compute x * ( 2 ^ n ) ; Driver Code
function multiply ( x , n ) { return x << n ; } let x = 70 ; let n = 2 ; document . write ( multiply ( x , n ) ) ;
Check if n is divisible by power of 2 without using arithmetic operators | function to check whether n is divisible by pow ( 2 , m ) ; if expression results to 0 , then n is divisible by pow ( 2 , m ) ; n is not divisible ; Driver program to test above
function isDivBy2PowerM ( n , m ) { if ( ( n & ( ( 1 << m ) - 1 ) ) == 0 ) return true ; return false ; } let n = 8 , m = 2 ; if ( isDivBy2PowerM ( n , m ) ) document . write ( " " ) ; else document . write ( " " ) ;
Game of Nim with removal of one stone allowed | Return true if player A wins , return false if player B wins . ; Checking the last bit of N . ; Driver code
function findWinner ( N ) { return N & 1 ; } let N = 15 ; if ( findWinner ( N ) == 1 ) document . write ( " " ) ; else document . write ( " " ) ;
Toggle all odd bits of a number | Returns a number which has all odd bits of n toggled . ; Generate number form of 101010. . . . . till of same order as n ; if bit is odd , then generate number and or with res ; return toggled number ; Driver code
function evenbittogglenumber ( n ) { let res = 0 , count = 0 ; for ( let temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 0 ) res |= ( 1 << count ) ; count ++ ; } return n ^ res ; } let n = 11 ; document . write ( evenbittogglenumber ( n ) ) ;
Quotient and remainder dividing by 2 ^ k ( a power of 2 ) | function to print remainder and quotient ; print Remainder by n AND ( m - 1 ) ; print quotient by right shifting n by ( log2 ( m ) ) times ; Driver code
function divide ( n , m ) { document . write ( " " + ( ( n ) & ( m - 1 ) ) + " " ) ; document . write ( " " + ( n >> ( Math . log ( m ) / Math . log ( 2 ) ) ) ) ; } let n = 43 , m = 8 ; divide ( n , m ) ;
Maximum AND value of a pair in an array | Function for finding maximum and value pair ; Driver function
function maxAND ( arr , n ) { var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j < n ; j ++ ) res = Math . max ( res , arr [ i ] & arr [ j ] ) ; return res ; } var arr = [ 4 , 8 , 6 , 2 ] ; var n = arr . length ; document . write ( " " + maxAND ( arr , n ) ) ;
Check if a number is positive , negative or zero using bit operators | function to return 1 if it is zero returns 0 if it is negative returns 2 if it is positive ; string array to store all kinds of number ; function call to check the sign of number ; driver program to test the above function
function index ( i ) { return 1 + ( i >> 31 ) - ( - i >> 31 ) ; } function check ( n ) { let s = [ " " , " " , " " ] ; let val = index ( n ) ; document . write ( n + " " + s [ val ] + " " ) ; } check ( 30 ) ; check ( - 20 ) ; check ( 0 ) ;
Divisibility by 64 with removal of bits allowed | function to check if it is possible to make it a multiple of 64. ; counter to count 0 's ; length of the string ; loop which traverses right to left and calculates the number of zeros before 1. ; Driver code
function checking ( s ) { let c = 0 ; let n = s . length ; for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == ' ' ) c ++ ; if ( c >= 6 && s [ i ] == ' ' ) return true ; } return false ; } let s = " " ; if ( checking ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Modify a bit at a given position | Returns modified n . ; Driver code
function modifyBit ( n , p , b ) { let mask = 1 << p ; return ( n & ~ mask ) | ( ( b << p ) & mask ) ; } document . write ( modifyBit ( 6 , 2 , 0 ) + " " ) ; document . write ( modifyBit ( 6 , 5 , 1 ) ) ;
Count set bits in a range | Function to get no of set bits in the binary representation of ' n ' ; function to count set 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 ; returns number of set bits in the range ' l ' to ' r ' in ' n ' ; driver program
function countSetBits ( n ) { let count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } function countSetBitsInGivenRange ( n , l , r ) { let num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return countSetBits ( n & num ) ; } let n = 42 ; let l = 2 , r = 5 ; document . write ( countSetBitsInGivenRange ( n , l , r ) ) ;
Check if one of the numbers is one 's complement of the other | 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 ; function to check if one of the two numbers is one 's complement  of the other ; Driver code
function areAllBitsSet ( n ) { if ( n == 0 ) return false ; if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } function isOnesComplementOfOther ( a , b ) { return areAllBitsSet ( a ^ b ) ; } let a = 10 , b = 5 ; if ( isOnesComplementOfOther ( a , b ) ) document . write ( " " ) ; else document . write ( " " ) ;
Unique element in an array where all elements occur k times except one | Javascript program to find unique element where every element appears k times except one ; Create a count array to store count of numbers that have a particular bit set . count [ i ] stores count of array elements with i - th bit set . ; AND ( bitwise ) each element of the array with each set digit ( one at a time ) to get the count of set bits at each position ; Now consider all bits whose count is not multiple of k to form the required number . ; driver function
function findUnique ( a , n , k ) { let sizeof_let = 4 ; let LET_SIZE = 8 * sizeof_let ; let count = Array . from ( { length : LET_SIZE } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < LET_SIZE ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) if ( ( a [ j ] & ( 1 << i ) ) != 0 ) count [ i ] += 1 ; let res = 0 ; for ( let i = 0 ; i < LET_SIZE ; i ++ ) res += ( count [ i ] % k ) * ( 1 << i ) ; return res ; } let a = [ 6 , 2 , 5 , 2 , 2 , 6 , 6 ] ; let n = a . length ; let k = 3 ; document . write ( findUnique ( a , n , k ) ) ;
Check whether the number has only first and last bits set | function to check whether ' n ' is a power of 2 or not ; function to check whether the number has only first and last bits set ; Driver program to test above
function powerOfTwo ( n ) { return ( ! ( n & n - 1 ) ) ; } function onlyFirstAndLastAreSet ( n ) { if ( n == 1 ) return true ; if ( n == 2 ) return false ; return powerOfTwo ( n - 1 ) ; } var n = 9 ; if ( onlyFirstAndLastAreSet ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a number has bits in alternate pattern | Set | function to check if all the bits are set or not in the binary representation of ' n ' ; if true , then all bits are set ; else all bits are not set ; function to check if a number has bits in alternate pattern ; to check if all bits are set in ' num ' ; driver function
function allBitsAreSet ( n ) { if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } function bitsAreInAltOrder ( n ) { let num = n ^ ( n >>> 1 ) ; return allBitsAreSet ( num ) ; } let n = 10 ; if ( bitsAreInAltOrder ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum flips required to maximize a number with k set bits | function for finding set bit ; return count of set bit ; function for finding min flip ; number of bits in n ; Find the largest number of same size with k set bits ; Count bit differences to find required flipping . ; driver program
function setBit ( xorValue ) { var count = 0 ; while ( xorValue ) { if ( xorValue % 2 ) count ++ ; xorValue = parseInt ( xorValue / 2 ) ; } return count ; } function minFlip ( n , k ) { var size = Math . log2 ( n ) + 1 ; var max = Math . pow ( 2 , k ) - 1 ; max = ( max << ( size - k ) ) ; var xorValue = ( n ^ max ) ; return ( setBit ( xorValue ) ) ; } var n = 27 , k = 3 ; document . write ( " " + minFlip ( n , k ) ) ;
Set all the bits in given range of a number | function to toggle bits in the given range ; calculating a number ' range ' having set bits in the range from l to r and all other bits as 0 ( or unset ) . ; Driver code
function setallbitgivenrange ( n , l , r ) { let range = ( ( ( 1 << ( l - 1 ) ) - 1 ) ^ ( ( 1 << ( r ) ) - 1 ) ) ; return ( n range ) ; } let n = 17 , l = 2 , r = 3 ; document . write ( setallbitgivenrange ( n , l , r ) ) ;
Count total bits in a number | JavaScript program to find total bit in given number ; log function in base 2 take only integer part ; Driven program
function countBits ( number ) { return Math . floor ( Math . log2 ( number ) + 1 ) ; } let num = 65 ; document . write ( countBits ( num ) ) ;