📌  相关文章
📜  翻转两个给定数字的设置位数的总和

📅  最后修改于: 2021-05-25 07:34:23             🧑  作者: Mango

给定两个数字AB ,任务是计算AB中设置的位数,然后翻转获得的总和的位数。

例子:

天真的方法:解决此问题的想法是先遍历两个数字的二进制表示形式,然后遍历两个数字中的设置位数。最后,将它们相加并将求和数的位取反。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Functon to count number of
// set bits in integer
int countSetBits(int n)
{
    // Variable for counting set bits
    int count = 0;
    while (n) {
        n &= (n - 1);
        count++;
    }
    return count;
}
 
// Function to invert bits of a number
int invertBits(int n)
{
    // Calculate number of bits of N-1;
    int x = log2(n);
 
    int m = 1 << x;
    m = m | m - 1;
    n = n ^ m;
 
    return n;
}
 
// Function to invert the sum
// of set bits in A and B
void invertSum(int A, int B)
{
 
    // Stores sum of set bits
    int temp = countSetBits(A)
               + countSetBits(B);
    cout << invertBits(temp) << endl;
}
 
// Driver Code
int main()
{
    int A = 5;
    int B = 7;
 
    invertSum(A, B);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
   
class GFG{
  
// Functon to count number of
// set bits in integer
static int countSetBits(int n)
{
     
    // Variable for counting set bits
    int count = 0;
     
    while (n != 0)
    {
        n &= (n - 1);
        count++;
    }
    return count;
}
  
// Function to invert bits of a number
static int invertBits(int n)
{
     
    // Calculate number of bits of N-1;
    int x = (int)(Math.log(n) / Math.log(2));
      
    int m = 1 << x;
    m = m | m - 1;
    n = n ^ m;
      
    return n;
}
  
// Function to invert the sum
// of set bits in A and B
static void invertSum(int A, int B)
{
     
    // Stores sum of set bits
    int temp = countSetBits(A) +
               countSetBits(B);
                 
    System.out.print(invertBits(temp));
}
   
// Driver Code
static public void main(String args[])
{
    int A = 5;
    int B = 7;
      
    invertSum(A, B);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
import math
 
# Functon to count number of
# set bits in integer
def countSetBits(n):
     
    # Variable for counting set bits
    count = 0
 
    while (n != 0):
        n &= (n - 1)
        count += 1
 
    return count
 
# Function to invert bits of a number
def invertBits(n):
     
    # Calculate number of bits of N-1;
    x = (int)(math.log(n) / math.log(2))
 
    m = 1 << x
    m = m | m - 1
    n = n ^ m
 
    return n
 
# Function to invert the sum
# of set bits in A and B
def invertSum(A, B):
     
    # Stores sum of set bits
    temp = countSetBits(A) + countSetBits(B)
 
    print(invertBits(temp))
 
# Driver Code
A = 5
B = 7
 
invertSum(A, B)
 
# This code is contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Functon to count number of
// set bits in integer
static int countSetBits(int n)
{
     
    // Variable for counting set bits
    int count = 0;
    while (n != 0)
    {
        n &= (n - 1);
        count++;
    }
    return count;
}
 
// Function to invert bits of a number
static int invertBits(int n)
{
     
    // Calculate number of bits of N-1;
    int x = (int)Math.Log(n, 2);
     
    int m = 1 << x;
    m = m | m - 1;
    n = n ^ m;
     
    return n;
}
 
// Function to invert the sum
// of set bits in A and B
static void invertSum(int A, int B)
{
     
    // Stores sum of set bits
    int temp = countSetBits(A) +
               countSetBits(B);
                
    Console.WriteLine(invertBits(temp));
}
 
// Driver Code
static void Main()
{
    int A = 5;
    int B = 7;
     
    invertSum(A, B);
}
}
 
// This code is contributed by divyesh072019


Javascript


输出:
2

时间复杂度: O(logN)
辅助空间: O(1)