Given an integer n, find the one’s complement of the integer.
Input : n = 5 Output : 2 Input : n = 255 Output : 0 Input : n = 26 Output : 5
An efficient approach to this problem is as follows:
1. Find the number of bits in the given integer
2. XOR the given integer with 2^number_of_bits-1
This article is contributed by Amit S K. 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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 8085 program to find 1's and 2's complement of 8-bit number
- 8085 program to find 1’s and 2’s complement of 16-bit number
- Find relative complement of two sorted arrays
- 8085 program to find 2's complement of the contents of Flag Register
- What’s difference between 1's Complement and 2's Complement?
- Find whether a given integer is a power of 3 or not
- Find closest integer with the same weight
- Find maximum XOR of given integer in a stream of integers
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Find single in an array of 2n+1 integer elements
- Find an integer X which is divisor of all except exactly one element in an array
- Find an integer in the given range that satisfies the given conditions
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Discrete logarithm (Find an integer k such that a^k is congruent modulo b)
- Find Quotient and Remainder of two integer without using division operators
Improved By : ihritik