检查其中一个数字是否是另一个的补码

给定两个非负整数 A. B .问题是检查这两个数字中的一个是否是1对另一个的补码。 这个 补语 一个二进制数的值被定义为将该数的二进制表示中的所有位倒置(将0替换为1,反之亦然)而获得的值。

null

例如:

Input : a = 10, b = 5Output : Yes(10)10 = (1010)21's complement of 10 is= (0101)2 = (101)2 = (5)10Input : a = 1, b = 14Output : Yes(14)10 = (1110)21'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 <bits/stdc++.h>
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

<?php
// PHP 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'
function 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
function isOnesComplementOfOther( $a ,
$b )
{
return areAllBitsSet( $a ^ $b );
}
// Driver Code
$a = 10; $b = 5;
if (isOnesComplementOfOther( $a , $b ))
echo "Yes" ;
else
echo "No" ;
// This code is contributed by anuj_67.
?>


Javascript

<script>
// Javascript 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'
function 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
function isOnesComplementOfOther(a, b)
{
return areAllBitsSet(a ^ b);
}
// Driver code
let a = 10, b = 5;
if (isOnesComplementOfOther(a,b))
document.write( "Yes" );
else
document.write( "No" );
</script>


输出:

Yes

本文由 阿尤什·焦哈里 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享