Given two non-negative integers N and K, the task is to invert the Kth most significant bit of N and print the number obtained after inverting the bit.
Input: N = 10, K = 1
The binary representation of 10 is 1010.
After inverting the first bit it becomes 0010
whose decimal equivalent is 2.
Input: N = 56, K = 2
Approach: Find the number of bits in N, if the number of bits is less than K then N itself is the required answer else flip the Kth most significant bit of N and print the number obtained after flipping it.
Below is the implementation of the above approach:
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.
- Toggle all bits after most significant bit
- Find most significant set bit of a number
- Find most significant bit of a number X in base Y
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Print 'K'th least significant bit of a number
- Round-off a number to a given number of significant digits
- Unset least significant K bits of a given number
- Invert actual bits of a number
- Program to invert bits of a number Efficiently
- Multiplication table till N rows where every Kth row is table of K upto Kth term
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Queries for number of array elements in a range with Kth Bit Set
- Smallest number exceeding N whose Kth bit is set
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Set the Left most unset bit
- Find position of left most dis-similar bit for two numbers
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Inserting m into n such that m starts at bit j and ends at bit i.
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.