Complement of Base 10 Integer - GeeksforGeeks

17 Jan, 2022
Given a base 10 integer N, the task is to find the 1’s complement of this Base 10 Integer.
Examples:
Input: N = 5Output: 2Explanation: Binary representation of 5 is “101”. Its one’s complement is “010” = 2.
Input: N = 255Output: 0
Approach: Here the number is converted by flipping bits and adding that power of 2 to the answer. Follow the steps mentioned below to implement it:
Find the binary representation of N.
For each bit, flip it and add the contribution of this bit to the final answer.
Return the final answer
Below is the implementation of the above approach.
C++
Java
Python
C#
Javascript
// C++ code to implement above approach#include <bits/stdc++.h>using namespace std; // Function to find the complementint findComplement(int num){    int ans = 0;    for (int i = 0; num > 0; i++) {        ans += pow(2, i) * (!(num % 2));        num /= 2;    }    return ans;} // Driver codeint main(){    unsigned int N = 5;    cout << findComplement(N);    return 0;}
// Java code to implement above approachclass GFG {   // Function to find the complement  static int findComplement(int num)  {    int ans = 0, x;    for (int i = 0; num > 0; i++) {      if (num % 2 == 1) {        x = 0;      }      else {        x = 1;      }      ans += (int)Math.pow(2, i) * x;      num /= 2;    }    return ans;  }   // Driver code  public static void main(String[] args)  {    int N = 5;    System.out.print(findComplement((int)N));  }} // This code is contributed by ukasp.
# Python code to implement above approach # Function to find the complementdef findComplement(num):    ans = 0;    x = 0;    i = 0;    while(num > 0):        if (num % 2 == 1):            x = 0;        else:            x = 1;         ans += pow(2, i) * x;        num //= 2;        i += 1;     return ans; # Driver codeif __name__ == '__main__':    N = 5;    print(findComplement(N)); # This code is contributed by 29AjayKumar
// C# code to implement above approachusing System;class GFG{   // Function to find the complement  static int findComplement(int num)  {    int ans = 0, x;    for (int i = 0; num > 0; i++) {      if(num % 2 == 1) {        x = 0;      }      else {        x = 1;      }      ans += (int)Math.Pow(2, i) * x;      num /= 2;    }    return ans;  }   // Driver code  public static void Main()  {    uint N = 5;    Console.Write(findComplement((int)N));   }} // This code is contributed by Samim Hossain Mondal.
<script>       // JavaScript code for the above approach        // Function to find the complement       function findComplement(num) {           let ans = 0;           for (let i = 0; num > 0; i++) {               ans += Math.pow(2, i) * (!(num % 2));               num = Math.floor(num / 2);           }           return ans;       }        // Driver code        let N = 5;       document.write(findComplement(N));  // This code is contributed by Potta Lokesh   </script>
  
2
 
Time Complexity: O(logN)Auxiliary Space: O(1)
 
lokeshpotta20
samim2000
ukasp
29AjayKumar
Bit Magic
Mathematical
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Swap two nibbles in a byte
Write an Efficient Method to Check if a Number is Multiple of 3
Highest power of 2 less than or equal to given number
Swap bits in a given number
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7