Given a number n, the task is to toggle only first and last bits of a number
Input : 10 Output : 3 Binary representation of 10 is 1010. After toggling first and last bits, we get 0011. Input : 15 Output : 6
Prerequisite : Find MSB of given number.
1) Generate a number which contains first and last bit as set. We need to change all middle bits to 0 and keep corner bits as 1.
2) Answer is XOR of generated number and original number.
Time Complexity is O(1).
- Toggle bits of a number except first and last bits
- Toggle all even bits of a number
- Toggle all the bits of a number except k-th bit.
- Toggle all odd bits of a number
- For every set bit of a number toggle bits of other
- Toggle the last m bits
- Toggle bits in the given range
- Toggle all bits after most significant bit
- Check if bits of a number has count of consecutive set bits in increasing order
- 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
- Same Number Of Set Bits As N
- Set all odd 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 email@example.com. 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