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:
Below is the implementation of the above approach:
C++
// C++ Program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the number of carry
// operations to add two binary numbers
int carryCount(int num1, int num2)
{
// To Store the carry count
int count = 0;
// Iterate till there is no carry
while (num2 != 0) {
// Carry now contains common
// set bits of x and y
int carry = num1 & num2;
// Sum of bits of x and y where at
// least one of the bits is not set
num1 = num1 ^ num2;
// Carry is shifted by one
// so that adding it to x
// gives the required sum
num2 = carry << 1;
// Adding number of 1's of
// carry to final count
count += __builtin_popcount(num2);
}
// Return the final count
return count;
}
// Driver Code
int main()
{
// Given two numbers
int A = 15, B = 10;
// Function Call
cout << carryCount(15, 10);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to count the number of carry
// operations to add two binary numbers
static int carryCount(int num1, int num2)
{
// To Store the carry count
int count = 0;
// Iterate till there is no carry
while (num2 != 0)
{
// Carry now contains common
// set bits of x and y
int carry = num1 & num2;
// Sum of bits of x and y where at
// least one of the bits is not set
num1 = num1 ^ num2;
// Carry is shifted by one
// so that adding it to x
// gives the required sum
num2 = carry << 1;
// Adding number of 1's of
// carry to final count
count += Integer.bitCount(num2);
}
// Return the final count
return count;
}
// Driver Code
public static void main(String[] args)
{
// Given two numbers
int A = 15, B = 10;
// Function call
System.out.print(carryCount(A, B));
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
# Function to count the number of carry
# operations to add two binary numbers
def carryCount(num1, num2):
# To Store the carry count
count = 0
# Iterate till there is no carry
while(num2 != 0):
# Carry now contains common
# set bits of x and y
carry = num1 & num2
# Sum of bits of x and y where at
# least one of the bits is not set
num1 = num1 ^ num2
# Carry is shifted by one
# so that adding it to x
# gives the required sum
num2 = carry << 1
# Adding number of 1's of
# carry to final count
count += bin(num2).count('1')
# Return the final count
return count
# Driver Code
# Given two numbers
A = 15
B = 10
# Function call
print(carryCount(A, B))
# This code is contributed by Shivam Singh
C#
// C# program for the above approach
using System;
class GFG{
static int countSetBits(int x)
{
int setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
// Function to count the number of carry
// operations to add two binary numbers
static int carryCount(int num1, int num2)
{
// To Store the carry count
int count = 0;
// Iterate till there is no carry
while (num2 != 0)
{
// Carry now contains common
// set bits of x and y
int carry = num1 & num2;
// Sum of bits of x and y where at
// least one of the bits is not set
num1 = num1 ^ num2;
// Carry is shifted by one
// so that adding it to x
// gives the required sum
num2 = carry << 1;
// Adding number of 1's of
// carry to readonly count
count += countSetBits(num2);
}
// Return the readonly count
return count;
}
// Driver Code
public static void Main(String[] args)
{
// Given two numbers
int A = 15, B = 10;
// Function call
Console.Write(carryCount(A, B));
}
}
// This code is contributed by Rohit_ranjan
JavaScript
<script>
// JavaScript program for the
// above approach
function countSetBits(x)
{
let setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
// Function to count the number of carry
// operations to add two binary numbers
function carryCount(num1, num2)
{
// To Store the carry count
let count = 0;
// Iterate till there is no carry
while (num2 != 0)
{
// Carry now contains common
// set bits of x and y
let carry = num1 & num2;
// Sum of bits of x and y where at
// least one of the bits is not set
num1 = num1 ^ num2;
// Carry is shifted by one
// so that adding it to x
// gives the required sum
num2 = carry << 1;
// Adding number of 1's of
// carry to readonly count
count += countSetBits(num2);
}
// Return the readonly count
return count;
}
// Driver Code
// Given two numbers
let A = 15, B = 10;
// Function call
document.write(carryCount(A, B));
</script>
Time Complexity: O(log2(N))
Auxiliary Space: O(1)
Similar Reads
Addition of two numbers without propagating Carry Given 2 numbers a and b of same length. The task is to calculate their sum in such a way that when adding two corresponding positions the carry has to be kept with them only instead of propagating to the left.See the below image for reference: Examples: Input: a = 7752 , b = 8834 Output: 151586 Inpu
15 min read
Adding one to number represented as array of digits Given a non-negative number represented as an array of digits. The is to add 1 to the number (increment the number represented by the digits by 1). The digits are stored such that the most significant digit is the first element of the array.Examples :Input : [1, 2, 4]Output : 125Explanation: 124 + 1
11 min read
Addition of two numbers without carry You are given two positive numbers n and m. You have to find a simple addition of both numbers but with a given condition that there is not any carry system in this addition. That is no carry is added at higher MSBs.Examples : Input : m = 456, n = 854 Output : 200 Input : m = 456, n = 4 Output : 450
6 min read
XOR of two numbers after making length of their binary representations equal Given two numbers say a and b. Print their XOR after making the lengths of their binary representation equal by adding trailing zeros to the binary representation of smaller one. Examples : Input : a = 13, b = 5 Output : 7 Explanation : Binary representation of 13 is 1101 and of 5 is 101. As the len
7 min read
XOR counts of 0s and 1s in binary representation Given a number, the task is to find XOR of count of 0s and count of 1s in binary representation of a given number. Examples: Input : 5 Output : 3 Binary representation : 101 Count of 0s = 1, Count of 1s = 2 1 XOR 2 = 3. Input : 7 Output : 3 Binary representation : 111 Count of 0s = 0 Count of 1s = 3
4 min read
Count the number of Binary Strings which have X 1's and Y 0's Given two integers X and Y, the task is to count the number of binary strings which have X 1's and Y 0's and there are no two consecutive 1's in the string. Examples: Input: X = 2, Y = 2Output: 3Explanation: 1010, 0101, 1001 - these are 3 strings that can be possible such that there are no two conse
9 min read