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 = 32
Output: 4294967294
Explanation:
1 in K(= 32) bit representation is (00000000000000000000000000000001)2.
Flipping all the bits modifies N to (11111111111111111111111111111110)2 = (4294967294)10.Input: N = 0, K = 32
Output: 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; } |
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 |
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 |
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 |
Javascript
<script> Â
// Javascript program of the above approach Â
// Function to flip all K-bits // of an unsigned number N function flippingBits(N, K) {           // Stores (2 ^ K) - 1     let X = (1 << (K - 1)) - 1;           // Update N     N = X - N;           // Print the answer     document.write(N); } Â
    // Driver Code        let N = 1, K = 8;           flippingBits(N, K);   </script> |
126
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!