Given a number, the task is to Toggle all even bit of a number
Input : 10 Output : 0 binary representation 1 0 1 0 after toggle 0 0 0 0 Input : 20 Output : 30 binary representation 1 0 1 0 0 after toggle 1 1 1 1 0
1. First generate a number that contains even position bits.
2. Take XOR with the original number. Note that 1 ^ 1 = 0 and 1 ^ 0 = 1.
Let’s understand this approach with below code.
- 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 first and last 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
- Set all the bits in given range of a number
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