- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Check if one of the numbers is one’s complement of the other in Python

Suppose we have two numbers x and y. We have to check whether one of these two numbers is 1's complement of the other or not. We all know the 1's complement of a binary number is flipping all bits from 0 to 1 or 1 to 0.

So, if the input is like x = 9, y = 6, then the output will be True as the binary representations are x = 1001 and y = 0110 which are complement of each other.

To solve this, we will follow these steps −

- z = x XOR y
- return true when all bits in z are set, otherwise false

## Example

Let us see the following implementation to get better understanding −

def all_one(n): if n == 0: return False; if ((n + 1) & n) == 0: return True return False def solve(x, y): return all_one(x ^ y) x = 9 y = 6 print(solve(x, y))

## Input

9, 6

## Output

True

- Related Questions & Answers
- Check if one list is subset of other in Python
- Check if one tuple is subset of other in Python
- One’s Complement
- Find One’s Complement of an Integer in C++
- Check if characters of one string can be swapped to form other in Python
- Check if bits in range L to R of two numbers are complement of each other or not in Python
- Check if max occurring character of one string appears same no. of times in other in Python
- MongoDB query to select one field if the other is null?
- Program to check whether one tree is subtree of other or not in Python
- C++ code to find pair of numbers where one is multiple of other
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string in Python
- Check if both halves of the string have at least one different character in Python
- Count Triplets such that one of the numbers can be written as sum of the other two in C++
- Check if edit distance between two strings is one in Python
- Find the Number of Substrings of One String Present in Other using C++

Advertisements