Given n number, the task is to toggle odd bit of the number.
Input : 10 Output : 15 binary representation 1 0 1 0 after toggle 1 1 1 1 Input : 20 Output : 1 binary representation 1 0 1 0 0 after toggle 0 0 0 0 1
1. First generate a number that contains odd 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Write an Efficient Method to Check if a Number is Multiple of 3
- Find the Number Occurring Odd Number of Times
- Write an Efficient C Program to Reverse Bits of a Number
- Count set bits in an integer
- Count number of bits to be flipped to convert A to B
- Rotate bits of a number
- Compute modulus division by a power-of-2-number
- Find whether a given number is a power of 4 or not
- Add 1 to a given number
- Next higher number with same number of set bits
- Program to count number of set bits in an (big) array
- Count total set bits in all numbers from 1 to n
- Swap bits in a given number
- Binary representation of a given number
- Swap all odd and even bits
- Check if a number is multiple of 9 using bitwise operators
- How to turn off a particular bit in a number?
- Check if binary representation of a number is palindrome
- Toggle all the bits of a number except k-th bit.
- Calculate square of a number without using *, / and pow()
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