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.
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.
- 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
- Check if all bits can be made same by flipping two consecutive bits
- 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
- Check whether bits are in alternate pattern in the given range | Set-2
- Check if bits in range L to R of two numbers are complement of each other or not
- Check if all the set bits of the binary representation of N are at least K places away
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Find the largest number smaller than integer N with maximum number of set bits
- Set all odd bits of a number
- Same Number Of Set Bits As N
Improved By : Smitha Dinesh Semwal