📌  相关文章
📜  检查其中一个数字是否是另一个的补码

📅  最后修改于: 2021-04-28 14:18:56             🧑  作者: Mango

给定两个非负整数ab 。问题是检查两个数字之一是否为另一个的1的补码。
二进制数的补码定义为通过将数字的二进制表示形式中的所有位取反得到的值(将0交换为1,反之亦然)。

例子:

Input : a = 10, b = 5
Output : Yes
(10)10 = (1010)2
1's complement of 10 is
= (0101)2 = (101)2 = (5)10

Input : a = 1, b = 14
Output : Yes
(14)10 = (1110)2
1's complement of 14 is
= (0001)2 = (1)2 = (1)10

方法:以下是步骤:

  1. 计算n = a ^ b。
  2. 检查是否以n的二进制表示形式设置了所有位。请参阅这篇文章。
CPP
// C++ implementation to check if one of the two
// numbers is one's complement of the other
#include 
using namespace std;
  
// function to check if all the bits are set
// or not in the binary representation of 'n'
bool areAllBitsSet(unsigned int n)
{
    // all bits are not set
    if (n == 0)
        return false;
   
    // if true, then all bits are set
    if (((n + 1) & n) == 0)
        return true;
   
    // else all bits are not set
    return false;
}
  
// function to check if one of the two numbers
// is one's complement of the other
bool isOnesComplementOfOther(unsigned int a, 
                             unsigned int b)
{
    return areAllBitsSet(a ^ b);
}
  
// Driver program to test above
int main()
{
    unsigned int a = 10, b = 5;
      
    if (isOnesComplementOfOther(a,b))
        cout << "Yes";
    else
        cout << "No";
          
    return 0;        
}


Java
// Java implementation to
// check if one of the two
// numbers is one's complement
// of the other
  
import java.util.*;
import java.lang.*;
  
public class GfG{
  
    // function to check
    // if all the bits are set
    // or not in the binary
    // representation of 'n'
    public static boolean areAllBitsSet(long n)
    {
        // all bits are not set
        if (n == 0)
            return false;
    
        // if true, then all bits are set
        if (((n + 1) & n) == 0)
            return true;
    
        // else all bits are not set
        return false;
    }
   
    // function to check if
    // one of the two numbers
    // is one's complement
    // of the other
    public static boolean isOnesComplementOfOther(long a, 
                             long b)
    {
        return areAllBitsSet(a ^ b);
    }
      
    // Driver function 
    public static void main(String argc[]){
        long a = 10, b = 5;
       
        if (isOnesComplementOfOther(a,b))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
           
}
  
// This code is contributed by Sagar Shukla


Python3
# Python3 implementation to 
# check if one of the two
# numbers is one's complement 
# of the other
  
  
# function to check if 
# all the bits are set
# or not in the binary 
# representation of 'n'
def areAllBitsSet(n):
      
    # all bits are not set
    if (n == 0):
        return False;
  
    # if True, then all bits are set
    if (((n + 1) & n) == 0):
        return True;
  
    # else all bits are not set
    return False;
  
  
# function to check if one 
# of the two numbers is
# one's complement of the other
def isOnesComplementOfOther(a, b):
  
    return areAllBitsSet(a ^ b)
  
  
# Driver program 
a = 1
b = 14
if (isOnesComplementOfOther(a, b)):
    print ("Yes")
else:
    print ("No")
          
# This code is contributed by 
# Saloni Gupta 4


C#
// C# implementation to check
// if one of the two numbers is
// one's complement of the other
using System;
  
class GFG {
  
    // function to check
    // if all the bits are set
    // or not in the binary
    // representation of 'n'
    public static bool areAllBitsSet(long n)
    {
        // all bits are not set
        if (n == 0)
            return false;
  
        // if true, then all bits are set
        if (((n + 1) & n) == 0)
            return true;
  
        // else all bits are not set
        return false;
    }
  
    // function to check if
    // one of the two numbers
    // is one's complement
    // of the other
    public static bool isOnesComplementOfOther(long a,
                                               long b)
    {
        return areAllBitsSet(a ^ b);
    }
  
    // Driver function
    public static void Main()
    {
        long a = 10, b = 5;
  
        if (isOnesComplementOfOther(a, b))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
  
// This code is contributed by Sam007


PHP


输出:

Yes