Comprueba si uno de los números es el complemento de uno del otro

Dados dos enteros no negativos a y b . El problema es comprobar si uno de los dos números es complemento a 1 del otro. 
El complemento a unos de un número binario se define como el valor obtenido al invertir todos los bits en la representación binaria del número (cambiando 0 por 1 y viceversa).

Ejemplos: 

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>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *