Given a number, the task is to toggle bits of the number except the first and the last bit.
Input : 10 Output : 12 Binary representation:- 1 0 1 0 After toggling first and last : 1 1 0 0 Input : 9 Output : 15 Binary representation : 1 0 0 1 After toggling first and last : 1 1 1 1
Prerequisite : Find most significant set bit of a number
1) Generate a number which contains middle bit as set. We need to change all middle bits to 1 and keep corner bits as 0.
2) Answer is XOR of generated number and original number. Note that XOR of 1 with a number toggles the number.
Time Complexity:- O(1)
- Toggle first and last bits of a number
- Toggle all even bits of a number
- Toggle all odd bits of a number
- Toggle all the bits of a number except k-th bit.
- 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
- Check if all bits can be made same by flipping two consecutive bits
- Print numbers having first and last bits as the only set bits
- Same Number Of Set Bits As N
- Set all odd bits of a number
- Set all even bits of a number
- Change all even bits in a number to 0