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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check whether the number has only first and last bits set
- Print numbers having first and last bits as the only set bits
- Toggle bits of a number except first and last bits
- Check if bits of a number has count of consecutive set bits in increasing order
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Check if a number has same number of set and unset bits
- Toggle first and last bits of a number
- Check if a number has bits in alternate pattern | Set 1
- Check if a number has two adjacent set bits
- Check if a number has bits in alternate pattern | Set-2 O(1) Approach
- First and Last Three Bits
- Number with set bits only between L-th and R-th index
- Check whether all the bits are set in the given range
- Check whether bits are in alternate pattern in the given range | Set-2
- Check if all bits can be made same by flipping two consecutive bits
- Check whether the two numbers differ at one bit position only
- 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
- Count pairs in an array such that both elements has equal set bits
Improved By : Smitha Dinesh Semwal