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.
Input:N = 1, K = 32
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
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:
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.