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 the bits of a number except k-th bit.
- For every set bit of a number toggle bits of other
- Toggle all odd bits of a number
- Toggle all even bits of a number
- Toggle the last m bits
- Toggle all bits after most significant bit
- Toggle bits in the given range
- 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 odd bits of a number
- Set all even bits of a number
- Same Number Of Set Bits As N
- Check if all bits of a number are set
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