📜  加两个二进制数时的进位运算计数

📅  最后修改于: 2021-05-25 04:27:58             🧑  作者: Mango

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

例子:

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

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

高效的方法:这个想法是使用 按位XOR和AND。步骤如下:

  • 使用XOR和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)