- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# 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 Articles
- Check if one list is subset of other in Python
- Check if one tuple is subset of other in Python
- The sum of two numbers is 16.25 if one of the numbers is 9.28, find the other
- The product of two numbers is 59.328 . If one of the numbers is 12.36 , Find the other number.
- The product of two rational numbers is 15. If one of the numbers is $-10$, find the other.
- The product of the two numbers is 76.35. If one of the numbers is 5, find the other number.
- The product of the two numbers is 80. If one of the numbers is 4 times the other. Find both numbers.
- Sum of two numbers is 95. If one exceeds the other by 15, find the numbers.
- If an angle is one-third of its complement, find the angle.
- The sum of two numbers is 95. If one of the numbers exceeds other number by15 then find the numbers?
- Check if bits in range L to R of two numbers are complement of each other or not in Python
- The sum of two rational numbers is $-8$. If one of the numbers is $−\frac{15}{7}$ find the other.
- The product of two decimal numbers is 131.58. If one of them is 2.15, find the other.
- If the sum of two numbers is 65 . If one of them is 30 , then find out the other?
- The sum of two numbers is $\frac{5}{9}$. If one of the numbers is $\frac{1}{3}$, find the other.

Advertisements