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: 11Output: 4 (11)_{10}= (1011)_{2}After inverting the bits, we get: (0100)_{2}= (4)_{10}.Input: 20Output: 11 (20)_{10}= (10100)_{2}. After inverting the bits, we get: (01011)_{2}= (11)_{10}.

The 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:

- Calculate the total number of bits in the given number. This can be done by calculating:
X = log

_{2}NWhere, N is the given number and X is the total number of bits of N.

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

- 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 <bits/stdc++.h> ` ` ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to invert actual ` `// bits of a number. ` ` ` `// Function to invert bits ` `// of a number ` `function` `invertBits(` `$n` `) ` `{ ` ` ` `// Calculate number of ` ` ` `// bits of N-1; ` ` ` `$x` `= log(` `$n` `, 2); ` ` ` ` ` `$m` `= 1 << ` `$x` `; ` ` ` ` ` `$m` `= ` `$m` `| ` `$m` `- 1; ` ` ` ` ` `$n` `= ` `$n` `^ ` `$m` `; ` ` ` ` ` `return` `$n` `; ` `} ` ` ` `// Driver code ` `$n` `= 20; ` ` ` `echo` `(invertBits(` `$n` `)); ` ` ` `// This code is contributed ` `// by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

11

**Time Complexity**: O(log_{2}n)

**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.

## Recommended Posts:

- Invert actual bits of a number
- Program to count number of set bits in an (big) array
- Program to find the Nth natural number with exactly two bits set
- Write an Efficient C Program to Reverse Bits of a Number
- Check if bits of a number has count of consecutive set bits in increasing order
- Finding the Parity of a number Efficiently
- Toggle bits of a number except first and last bits
- Invert the Kth most significant bit of N
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Find the largest number smaller than integer N with maximum number of set bits
- Maximum number of contiguous array elements with same number of set bits
- Check if a number has same number of set and unset bits
- Set all even bits of a number
- Set all odd bits of a number
- Same Number Of Set Bits As N
- Number with set bits only between L-th and R-th index
- Toggle all even bits of a number
- For every set bit of a number toggle bits of other
- Toggle first and last bits of a number
- Check if all bits of a number are set

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.