Count of carry operations on adding two Binary numbers | JavaScript program for the above approach ; Function to count the number of carry operations to add two binary numbers ; To Store the carry count ; Iterate till there is no carry ; Carry now contains common set bits of x and y ; Sum of bits of x and y where at least one of the bits is not set ; Carry is shifted by one so that adding it to x gives the required sum ; Adding number of 1 's of  carry to readonly count ; Return the readonly count ; Given two numbers ; Function call
function countSetBits ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } function carryCount ( num1 , num2 ) { let count = 0 ; while ( num2 != 0 ) { let carry = num1 & num2 ; num1 = num1 ^ num2 ; num2 = carry << 1 ; count += countSetBits ( num2 ) ; } return count ; } let A = 15 , B = 10 ; document . write ( carryCount ( A , B ) ) ;