Check if one of the numbers is one's complement of the other - GeeksforGeeks

28 Apr, 2021
Given two non-negative integers a and b. The problem is to check if one of the two numbers is 1’s complement of the other. The ones’ complement of a binary number is defined as the value obtained by inverting all the bits in the binary representation of the number (swapping 0s for 1s and vice versa).
Examples: 
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
Approach: Following are the steps: 
Calculate n = a ^ b.Check whether all bits are set in the binary representation of n. Refer to this post. 
Calculate n = a ^ b.
Check whether all bits are set in the binary representation of n. Refer to this post. 
CPP
Java
Python3
C#
PHP
Javascript
// 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 otherbool isOnesComplementOfOther(unsigned int a,                             unsigned int b){    return areAllBitsSet(a ^ b);} // Driver program to test aboveint main(){    unsigned int a = 10, b = 5;         if (isOnesComplementOfOther(a,b))        cout << "Yes";    else        cout << "No";             return 0;       }
// 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 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 otherdef isOnesComplementOfOther(a, b):     return areAllBitsSet(a ^ b)  # Driver programa = 1b = 14if (isOnesComplementOfOther(a, b)):    print ("Yes")else:    print ("No")         # This code is contributed by# Saloni Gupta 4
// C# implementation to check// if one of the two numbers is// one's complement of the otherusing 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 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 otherfunction 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.?>
<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>
Output: 
Yes
YouTubeGeeksforGeeks506K subscribersCheck if one of the numbers is one’s complement of the other | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:33•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=DHePoeaprNY" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
sanjoy_62
Bitwise-XOR
complement
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Little and Big Endian Mystery
Cyclic Redundancy Check and Modulo-2 Division
Binary representation of a given number
Program to find whether a given number is power of 2
Add two numbers without using arithmetic operators
Set, Clear and Toggle a given bit of a number in C
Josephus problem | Set 1 (A O(n) Solution)
Bit Fields in C
Find the element that appears once
1's and 2's complement of a Binary Number