Related Articles

# Program to invert bits of a number Efficiently

• Difficulty Level : Medium
• Last Updated : 28 Apr, 2021

Given a non-negative integer N. The task is to invert the bits of the number N and print the decimal equivalent of the number obtained after inverting the bits.
Note: Leading 0’s are not being considered.
Examples:

```Input : 11
Output : 4
(11)10 = (1011)2
After inverting the bits, we get:
(0100)2 = (4)10.

Input : 20
Output : 11
(20)10 = (10100)2.
After inverting the bits, we get:
(01011)2 = (11)10.```

A similar problem is already discussed in Invert actual bits of a number.
In this article, an efficient approach using bitwise operators is discussed. Below is the step by step algorithm to solve the problem:

1. Calculate the total number of bits in the given number. This can be done by calculating:
`X = log2N`

Where N is the given number and X is the total number of bits of N.

2. The next step is to generate a number with X bits and all bits set. That is, 11111….X-times. This can be done by calculating:
```Step-1: M = 1 << X
Step-2: M = M | (M-1)```

Where M is the required X-bit number with all bits set.

3. The final step is to calculate the bit-wise XOR of M with N, which will be our answer.

Below is the implementation of the above approach:

## C++

 `// C++ program to invert actual bits``// of a number.``#include `` ` `using` `namespace` `std;`` ` `// Function to invert bits of a number``int` `invertBits(``int` `n)``{``    ``// Calculate number of bits of N-1;``    ``int` `x = log2(n) ;`` ` `    ``int` `m = 1 << x;`` ` `    ``m = m | m - 1;`` ` `    ``n = n ^ m;`` ` `    ``return` `n;``}`` ` `// Driver code``int` `main()``{``    ``int` `n = 20;`` ` `    ``cout << invertBits(n);`` ` `    ``return` `0;``}`

## Java

 `// Java program to invert ``// actual bits of a number. ``import` `java.util.*;`` ` `class` `GFG``{``// Function to invert ``// bits of a number ``static` `int` `invertBits(``int` `n) ``{ ``    ``// Calculate number of bits of N-1; ``    ``int` `x = (``int``)(Math.log(n) / ``                  ``Math.log(``2``)) ; `` ` `    ``int` `m = ``1` `<< x; `` ` `    ``m = m | m - ``1``; `` ` `    ``n = n ^ m; `` ` `    ``return` `n; ``} `` ` `// Driver code ``public` `static` `void` `main(String[] args) ``{ ``    ``int` `n = ``20``; `` ` `    ``System.out.print(invertBits(n)); ``}``} `` ` `// This code is contributed by Smitha`

## Python3

 `# Python3 program to invert actual``# bits of a number.``import` `math`` ` `# Function to invert bits of a number``def` `invertBits(n):``     ` `    ``# Calculate number of bits of N-1``    ``x ``=` `int``(math.log(n, ``2``))`` ` `    ``m ``=` `1` `<< x`` ` `    ``m ``=` `m | m ``-` `1`` ` `    ``n ``=` `n ^ m`` ` `    ``return` `n`` ` `# Driver code``n ``=` `20`` ` `print``(invertBits(n))`` ` `# This code is contributed 29AjayKumar`

## C#

 `// C# program to invert ``// actual bits of a number. ``using` `System;`` ` ` ` `  ` `public` `class` `GFG``{``// Function to invert ``// bits of a number ``static` `int` `invertBits(``int` `n) ``{ ``    ``// Calculate number of bits of N-1; ``    ``int` `x = (``int``)(Math.Log(n) / ``                  ``Math.Log(2)) ; ``  ` `    ``int` `m = 1 << x; ``  ` `    ``m = m | m - 1; ``  ` `    ``n = n ^ m; ``  ` `    ``return` `n; ``} ``  ` `// Driver code ``public` `static` `void` `Main() ``{ ``    ``int` `n = 20; ``  ` `    ``Console.Write(invertBits(n)); ``}``} ``  ` `// This code is contributed by Subhadeep`

## PHP

 ``

## Javascript

 ``
Output:
`11`

Time Complexity: O(log2n)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up