📜  最大化表达式(A AND X)*(B AND X)|位操作

📅  最后修改于: 2021-06-25 22:45:57             🧑  作者: Mango

给定两个正整数AB使得A!= B ,任务是找到一个使表达式(A AND X)*(B AND X)最大化的正整数X。
例子:

天真的方法:可以运行从1max(A,B)的循环并且可以轻松地找到使给定表达式最大化的X。
高效的方法:众所周知,

现在X可以找到为:

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
#define MAX 32
 
// Function to find X according
// to the given conditions
int findX(int A, int B)
{
    int X = 0;
 
    // int can have 32 bits
    for (int bit = 0; bit < MAX; bit++) {
 
        // Temporary ith bit
        int tempBit = 1 << bit;
 
        // Compute ith bit of X according to
        // given conditions
        // Expression below is the direct
        // conclusion from the illustration
        // we had taken earlier
        int bitOfX = A & B & tempBit;
 
        // Add the ith bit of X to X
        X += bitOfX;
    }
 
    return X;
}
 
// Driver code
int main()
{
    int A = 11, B = 13;
 
    cout << findX(A, B);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
static int MAX = 32;
 
// Function to find X according
// to the given conditions
static int findX(int A, int B)
{
    int X = 0;
 
    // int can have 32 bits
    for (int bit = 0; bit < MAX; bit++)
    {
 
        // Temporary ith bit
        int tempBit = 1 << bit;
 
        // Compute ith bit of X according to
        // given conditions
        // Expression below is the direct
        // conclusion from the illustration
        // we had taken earlier
        int bitOfX = A & B & tempBit;
 
        // Add the ith bit of X to X
        X += bitOfX;
    }
    return X;
}
 
// Driver code
public static void main(String []args)
{
    int A = 11, B = 13;
 
    System.out.println(findX(A, B));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
MAX = 32
 
# Function to find X according
# to the given conditions
def findX(A, B) :
 
    X = 0;
 
    # int can have 32 bits
    for bit in range(MAX) :
 
        # Temporary ith bit
        tempBit = 1 << bit;
 
        # Compute ith bit of X according to
        # given conditions
        # Expression below is the direct
        # conclusion from the illustration
        # we had taken earlier
        bitOfX = A & B & tempBit;
 
        # Add the ith bit of X to X
        X += bitOfX;
 
    return X;
 
# Driver code
if __name__ == "__main__" :
     
    A = 11; B = 13;
    print(findX(A, B));
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
     
class GFG
{
static int MAX = 32;
 
// Function to find X according
// to the given conditions
static int findX(int A, int B)
{
    int X = 0;
 
    // int can have 32 bits
    for (int bit = 0; bit < MAX; bit++)
    {
 
        // Temporary ith bit
        int tempBit = 1 << bit;
 
        // Compute ith bit of X according to
        // given conditions
        // Expression below is the direct
        // conclusion from the illustration
        // we had taken earlier
        int bitOfX = A & B & tempBit;
 
        // Add the ith bit of X to X
        X += bitOfX;
    }
    return X;
}
 
// Driver code
public static void Main(String []args)
{
    int A = 11, B = 13;
 
    Console.WriteLine(findX(A, B));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
9

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。