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
Basic Approach :
The naïve approach to solve the problem would be to first convert the given number into its binary representation and then change every 1’s to 0 and 0’s to 1. After changing all 0’s and 1’s convert the binary representation to number.
Implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
unsigned int onesComplement(unsigned int n)
{
vector< int > v;
while (n != 0) {
v.push_back(n % 2);
n = n / 2;
}
reverse(v.begin(), v.end());
for ( int i = 0; i < v.size(); i++) {
if (v[i] == 0)
v[i] = 1;
else
v[i] = 0;
}
int two = 1;
for ( int i = v.size() - 1; i >= 0; i--) {
n = n + v[i] * two;
two = two * 2;
}
return n;
}
int main()
{
unsigned int n = 22;
cout << onesComplement(n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int onesComplement( int n)
{
ArrayList<Integer> v = new ArrayList<Integer>();
while (n != 0 ) {
v.add(n % 2 );
n = n / 2 ;
}
Collections.reverse(v);
for ( int i = 0 ; i < v.size(); i++) {
if (v.get(i) == 0 )
v.set(i, 1 );
else
v.set(i, 0 );
}
int two = 1 ;
for ( int i = v.size() - 1 ; i >= 0 ; i--) {
n = n + v.get(i) * two;
two = two * 2 ;
}
return n;
}
public static void main(String[] args)
{
int n = 22 ;
System.out.println(onesComplement(n));
}
}
|
Python3
def onesComplement(n):
v = []
while (n ! = 0 ):
v.append(n % 2 )
n = n / / 2
v.reverse()
for i in range ( len (v)):
if (v[i] = = 0 ):
v[i] = 1
else :
v[i] = 0
two = 1
for i in range ( len (v) - 1 , - 1 , - 1 ):
n = n + v[i] * two
two = two * 2
return n
n = 22
print (onesComplement(n))
|
C#
using System;
using System.Collections;
public class GFG{
public static int onesComplement( int n)
{
ArrayList v = new ArrayList();
while (n != 0) {
v.Add(n % 2);
n = n / 2;
}
v.Reverse();
for ( int i = 0; i < v.Count; i++) {
if (Convert.ToInt32(v[i]) == 0)
v[i]= 1;
else
v[i]= 0;
}
int two = 1;
for ( int i = v.Count - 1; i >= 0; i--) {
n = n + Convert.ToInt32(v[i]) * two;
two = two * 2;
}
return n;
}
static public void Main (){
int n = 22;
Console.WriteLine(onesComplement(n));
}
}
|
Javascript
function onesComplement(n)
{
let v = [];
while (n != 0) {
v.push(n % 2);
n = Math.floor(n / 2);
}
v.reverse();
for ( var i = 0; i < v.length; i++) {
if (v[i] == 0)
v[i] = 1;
else
v[i] = 0;
}
let two = 1;
for (let i = v.length - 1; i >= 0; i--) {
n = n + v[i] * two;
two = two * 2;
}
return n;
}
let n = 22;
console.log(onesComplement(n));
|
Time Complexity : O(log n)
Auxiliary Space : O(log n)
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++
#include <bits/stdc++.h>
using namespace std;
unsigned int onesComplement(unsigned int n)
{
int number_of_bits = floor (log2(n)) + 1;
return ((1 << number_of_bits) - 1) ^ n;
}
int main()
{
unsigned int n = 22;
cout << onesComplement(n);
return 0;
}
|
Java
class GFG {
static int onesComplement( int n)
{
int number_of_bits
= ( int )(Math.floor(Math.log(n) / Math.log( 2 )))
+ 1 ;
return (( 1 << number_of_bits) - 1 ) ^ n;
}
public static void main(String[] args)
{
int n = 22 ;
System.out.print(onesComplement(n));
}
}
|
Python3
import math
def onesComplement(n):
number_of_bits = ( int )(math.floor(math.log(n) /
math.log( 2 ))) + 1
return (( 1 << number_of_bits) - 1 ) ^ n
n = 22
print (onesComplement(n))
|
C#
using System;
class GFG {
static int onesComplement( int n)
{
int number_of_bits
= ( int )(Math.Floor(Math.Log(n) / Math.Log(2)))
+ 1;
return ((1 << number_of_bits) - 1) ^ n;
}
public static void Main()
{
int n = 22;
Console.WriteLine(onesComplement(n));
}
}
|
PHP
<?php
function Log2( $x )
{
return (log10( $x ) / log10(2));
}
function onesComplement( $n )
{
$number_of_bits = floor (log2( $n )) + 1;
return ((1 << $number_of_bits ) - 1) ^ $n ;
}
$n = 22;
echo onesComplement( $n );
?>
|
Javascript
<script>
function onesComplement(n)
{
let number_of_bits =
(Math.floor(Math.log(n) /
Math.log(2))) + 1;
return ((1 << number_of_bits) - 1) ^ n;
}
let n = 22;
document.write(onesComplement(n));
</script>
|
Time Complexity : O(log n)
Auxiliary Space : O(1)
Last Updated :
30 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...