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++

filter_none

editclose

play_arrow

linkbrightness_4code

// CPP program to find 1's complement of n.

#include<bits/stdc++.h>

usingnamespacestd;

unsigned intonesComplement(unsigned intn)

{

// Find number of bits in the given integer

intnumber_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;

}

intmain()

{

unsigned intn = 22;

cout << onesComplement(n);

return0;

}

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to find 1's complement of n.

classGFG {

staticintonesComplement(intn)

{

// Find number of bits in the

// given integer

intnumber_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

publicstaticvoidmain(String[] args)

{

intn = 22;

System.out.print(onesComplement(n));

}

}

// This code is contributed by Anant Agarwal.

chevron_right

filter_none

Python3

filter_none

editclose

play_arrow

linkbrightness_4code

# Python3 program to find

# 1's complement of n.

importmath

defonesComplement(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.

chevron_right

filter_none

C#

filter_none

editclose

play_arrow

linkbrightness_4code

// C# program to find 1's complement of n.

usingSystem;

classGFG {

staticintonesComplement(intn)

{

// Find number of bits in the given integer

intnumber_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

publicstaticvoidMain ()

{

intn = 22;

Console.WriteLine(onesComplement(n));

}

}

// This code is contributed by Anant Agarwal.

chevron_right

filter_none

PHP

filter_none

editclose

play_arrow

linkbrightness_4code

<?php

// PHP program to find 1's

// complement of n.

functionLog2($x)

{

return(log10($x) / log10(2));

}

functiononesComplement($n)

{

// Find number of bits in

// the given integer

$number_of_bits= floor(log2($n)) + 1;

// XOR the given integer with

// number_of_bits-1 and print the result

return((1 << $number_of_bits) - 1) ^ $n;

}

// Driver Code

$n= 22;

echoonesComplement($n);

// This code is contributed by ihritik

?>

chevron_right

filter_none

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.

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