Count of carry operations on adding two Binary numbers | 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 final count ; Return the final count ; Given two numbers ; Function call
def carryCount ( num1 , num2 ) : NEW_LINE INDENT count = 0 NEW_LINE while ( num2 != 0 ) : NEW_LINE INDENT carry = num1 & num2 NEW_LINE num1 = num1 ^ num2 NEW_LINE num2 = carry << 1 NEW_LINE count += bin ( num2 ) . count ( '1' ) NEW_LINE DEDENT return count NEW_LINE DEDENT A = 15 NEW_LINE B = 10 NEW_LINE print ( carryCount ( A , B ) ) NEW_LINE