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).
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 this post.
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 firstname.lastname@example.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.
- Check if bits in range L to R of two numbers are complement of each other or not
- Subtraction of two numbers using 2's Complement
- Why are negative numbers stored as 2's complement?
- Check if binary representation of a given number and its complement are anagram
- What’s difference between 1's Complement and 2's Complement?
- 1's and 2's complement of a Binary Number
- 9's complement of a decimal number
- Find One's Complement of an Integer
- Previous number same as 1's complement
- Complement of a number with any base b
- 10's Complement of a decimal number
- 8085 program to find 1’s and 2’s complement of 16-bit number
- Efficient method for 2's complement of a binary string
- Find relative complement of two sorted arrays
- 8085 program to find 1's and 2's complement of 8-bit number
Improved By : vt_m