Count of carry operations on adding two Binary numbers
Last Updated :
07 May, 2021
Given two decimal numbers num1 and num2, the task is to count the number of times carry operation is required while adding the two given numbers in binary form.
Examples:
Input: num1 = 15, num2 = 10
Output: 3
Explanation:
Give numbers are added as:
15 -> 1 1 1 1
10 -> 1 0 1 0
carry -> 1 1 1 – –
——————————
25 -> 1 1 0 0 1
Input: num1 = 14 num2 = 4
Output: 2
Explanation:
Give numbers are added as:
14 -> 1 1 1 0
4 -> 0 1 0 0
carry -> 1 1 – – –
——————————
18 -> 1 0 0 1 0
Naive Approach: The naive idea is to convert the numbers into binary and add a bit one by one starting from Least Significant Bit and check if carry is generated or not. Whenever a carry is generated then increase the count by 1. Print count of carry after all the steps.
Time Complexity: O(K), where K is a count of the number of digits in the binary representation of X.
Auxiliary Space: O(log N)
Efficient Approach: The idea is to use Bitwise XOR and AND. Below are the steps:
- Add the two binary numbers using XOR and AND.
- Now, the number of 1’s in the Bitwise AND of two numbers shows the number of carry bits at that step.
- Add the number of one’s in each stage in the above step to get the final count of carry operation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
class GFG{
static int carryCount( int num1, int num2)
{
int count = 0 ;
while (num2 != 0 )
{
int carry = num1 & num2;
num1 = num1 ^ num2;
num2 = carry << 1 ;
count += Integer.bitCount(num2);
}
return count;
}
public static void main(String[] args)
{
int A = 15 , B = 10 ;
System.out.print(carryCount(A, B));
}
}
|
Python3
def carryCount(num1, num2):
count = 0
while (num2 ! = 0 ):
carry = num1 & num2
num1 = num1 ^ num2
num2 = carry << 1
count + = bin (num2).count( '1' )
return count
A = 15
B = 10
print (carryCount(A, B))
|
C#
using System;
class GFG{
static int countSetBits( int x)
{
int setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
static int carryCount( int num1, int num2)
{
int count = 0;
while (num2 != 0)
{
int carry = num1 & num2;
num1 = num1 ^ num2;
num2 = carry << 1;
count += countSetBits(num2);
}
return count;
}
public static void Main(String[] args)
{
int A = 15, B = 10;
Console.Write(carryCount(A, B));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(log2(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...