Given a positive integer n, check whether only the first and last bits are set in the binary representation of n. Print ‘Yes’ or ‘No’.
(9)10 = (1001)2, only the first and
last bits are set.
(15)10 = (1111)2, except first and last
there are other bits also which are set.
We have already discussed a solution here.
In this post, a simpler solution is discussed.
- Check if bits of a number has count of consecutive set bits in increasing order
- Check if all bits of a number are set
- Check whether the number has only first and last bits set
- Check if a number has two adjacent set bits
- Check if a number has bits in alternate pattern | Set 1
- Check if a number has same number of set and unset bits
- Check if a number has bits in alternate pattern | Set-2 O(1) Approach
- Toggle bits of a number except first and last bits
- Check whether all the bits are set in the given range
- Check whether all the bits are unset in the given range or not
- Check whether all the bits are unset in the given range
- Check whether bits are in alternate pattern in the given range | Set-2
- Check whether bits are in alternate pattern in the given range
- Check if bits in range L to R of two numbers are complement of each other or not
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
Improved By : Smitha Dinesh Semwal