Given a non-negative integer n. The problem is to increment n by 1 by manipulating the bits of n.
Input : 6 Output : 7 Input : 15 Output : 16
Approach: Following are the steps:
- Get the position of rightmost unset bit of n. Let this position be k.
- Set the k-th bit of n.
- Toggle the last k-1 bits of n.
- Finally, return n.
- Check if bits of a number has count of consecutive set bits in increasing order
- Increment a number without using ++ or +
- Toggle bits of a number except first and last bits
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- 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
- Check whether the number has only first and last bits set
- Largest number less than X having at most K set bits
- Check whether the number has only first and last bits set | Set 2
- Toggle all the bits of a number except k-th bit.
- Toggle all odd bits of a number
- Toggle all even bits of a number
- Number with set bits only between L-th and R-th index
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.
Improved By : Mithun Kumar