Given two integers **N **and **K**, the task is to represent **N** in **K** bits and print the number obtained after flipping all the bits.

**Examples:**

Input:N = 1, K = 32Output:4294967294Explanation:

1 in K(= 32) bit representation is (00000000000000000000000000000001)_{2}.

Flipping all the bits modifies N to (11111111111111111111111111111110)_{2}= (4294967294)_{10}.

Input:N = 0, K = 32Output:4294967295

**Approach: **Follow the steps below to solve the problem:

- Find the value of
**(1 << (K – 1)) – 1**, say**X**. - Finally, print the value of
**(X – N)**.

Below is the implementation of the above approach:

## C++

`// C++ Program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to flip all K-bits` `// of an unsigned number N` `void` `flippingBits(unsigned ` `long` `N,` ` ` `unsigned ` `long` `K)` `{` ` ` `// Stores (2 ^ K) - 1` ` ` `unsigned ` `long` `X = (1 << (K - 1)) - 1;` ` ` `// Update N` ` ` `N = X - N;` ` ` `// Print the answer` ` ` `cout << N;` `}` `// Driver Code` `int` `main()` `{` ` ` `unsigned ` `long` `N = 1, K = 8;` ` ` `flippingBits(N, K);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG{` `// Function to flip all K-bits` `// of an unsigned number N` `static` `void` `flippingBits(` `long` `N,` ` ` `long` `K)` `{` ` ` ` ` `// Stores (2 ^ K) - 1` ` ` `long` `X = (` `1` `<< (K - ` `1` `)) - ` `1` `;` ` ` ` ` `// Update N` ` ` `N = X - N;` ` ` ` ` `// Print the answer` ` ` `System.out.print(N);` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `long` `N = ` `1` `, K = ` `8` `;` ` ` ` ` `flippingBits(N, K);` `}` `}` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program for the above approach` `# Function to flip all K-bits` `# of an unsigned number N` `def` `flippingBits(N, K):` ` ` `# Stores (2 ^ K) - 1` ` ` `X ` `=` `(` `1` `<< (K ` `-` `1` `)) ` `-` `1` ` ` `# Update N` ` ` `N ` `=` `X ` `-` `N` ` ` `# Print the answer` ` ` `print` `(N)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `N, K ` `=` `1` `, ` `8` ` ` `flippingBits(N, K)` ` ` `# This code is contribute by mohit kumar 29` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Function to flip all K-bits` `// of an unsigned number N` `static` `void` `flippingBits(` `int` `N, ` `int` `K)` `{` ` ` ` ` `// Stores (2 ^ K) - 1` ` ` `int` `X = (1 << (K - 1)) - 1;` ` ` `// Update N` ` ` `N = X - N;` ` ` `// Print the answer` ` ` `Console.Write(N);` `}` `// Driver Code` `public` `static` `void` `Main(` `string` `[] args)` `{` ` ` `int` `N = 1, K = 8;` ` ` ` ` `flippingBits(N, K);` `}` `}` `// This code is contributed by chitranayal` |

*chevron_right*

*filter_none*

**Output:**

126

**Time Complexity:** O(1)**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.