Count of carry operations on adding two Binary numbers | C ++ 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 final count ; Return the final count ; Driver Code ; Given two numbers ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int carryCount ( int num1 , int num2 ) { int count = 0 ; while ( num2 != 0 ) { int carry = num1 & num2 ; num1 = num1 ^ num2 ; num2 = carry << 1 ; count += __builtin_popcount ( num2 ) ; } return count ; } int main ( ) { int A = 15 , B = 10 ; cout << carryCount ( 15 , 10 ) ; return 0 ; }