📌  相关文章
📜  两个二进制数相加的进位运算次数

📅  最后修改于: 2021-10-26 05:18:58             🧑  作者: Mango

给定两个十进制数num1num2 ,任务是计算将两个给定数字以二进制形式相加时需要进行进位运算的次数。

例子:

朴素的方法:朴素的想法是将数字转换为二进制并从最低有效位开始一位一位地添加并检查是否产生进位。每当产生进位时,将计数加 1。在所有步骤之后打印进位计数。

时间复杂度: O(K),其中 K 是 X 的二进制表示中位数的计数。
辅助空间: O(log N)

高效的方法:这个想法是使用 按位异或和与。以下是步骤:

  • 使用 XOR 和 AND 将两个二进制数相加。
  • 现在,两个数字的按位 AND 中 1 的数量显示了该步骤的位位数。
  • 将上述步骤中每一阶段的个数相加,得到最终的进位计数。

下面是上述方法的实现:

C++
// C++ Program for the above approach
#include 
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


输出:
3

时间复杂度: O(log 2 (N))
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程