# Find One’s Complement of an Integer

Given an integer n, find the one’s complement of the integer.

Examples:

```Input  : n = 5
Output : 2

Input  : n = 255
Output : 0

Input  : n = 26
Output : 5
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

## C++

 `// CPP program to find 1's complement of n. ` `#include ` `using` `namespace` `std; ` ` `  `unsigned ``int` `onesComplement(unsigned ``int` `n) ` `{ ` `   ``// Find number of bits in the given integer ` `   ``int` `number_of_bits = ``floor``(log2(n))+1; ` ` `  `   ``// XOR the given integer with poe(2,  ` `   ``// number_of_bits-1 and print the result  ` `   ``return` `((1 << number_of_bits) - 1) ^ n; ` `} ` ` `  `int` `main() ` `{ ` `  ``unsigned ``int` `n = 22; ` `  ``cout << onesComplement(n); ` `  ``return` `0; ` `}  `

## Java

 `// Java program to find 1's complement of n. ` `class` `GFG { ` `     `  `    ``static` `int` `onesComplement(``int` `n) ` `    ``{ ` `         `  `        ``// Find number of bits in the  ` `        ``// given integer ` `        ``int` `number_of_bits =  ` `               ``(``int``)(Math.floor(Math.log(n) / ` `                             ``Math.log(``2``))) + ``1``; ` ` `  `        ``// XOR the given integer with poe(2, ` `        ``// number_of_bits-1 and print the result ` `        ``return` `((``1` `<< number_of_bits) - ``1``) ^ n; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``22``; ` `         `  `        ``System.out.print(onesComplement(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python3

 `# Python3 program to find ` `# 1's complement of n. ` `import` `math ` ` `  `def` `onesComplement(n): ` ` `  `    ``# Find number of bits in ` `    ``# the given integer ` `    ``number_of_bits ``=` `(``int``)(math.floor(math.log(n) ``/` `                                ``math.log(``2``))) ``+` `1``; ` ` `  `    ``# XOR the given integer with poe(2,  ` `    ``# number_of_bits-1 and print the result  ` `    ``return` `((``1` `<< number_of_bits) ``-` `1``) ^ n; ` ` `  `# Driver code ` `n ``=` `22` `print``(onesComplement(n)) ` ` `  `# This code is contributed by Anant Agarwal. `

## C#

 `// C# program to find 1's complement of n. ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``static` `int` `onesComplement(``int` `n) ` `    ``{ ` `         `  `       ``// Find number of bits in the given integer ` `       ``int` `number_of_bits = (``int``)(Math.Floor( ` `                   ``Math.Log(n) / Math.Log(2))) + 1; ` `      `  `       ``// XOR the given integer with poe(2,  ` `       ``// number_of_bits-1 and print the result  ` `       ``return` `((1 << number_of_bits) - 1) ^ n; ` `    ``} ` `     `  `    ``//Driver code ` `    ``public` `static` `void` `Main () ` `    ``{ ` `         `  `        ``int` `n = 22; ` `         `  `        ``Console.WriteLine(onesComplement(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## PHP

 `  `

Output :

```9
```

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 contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Improved By : ihritik

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.