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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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
- Check whether all the bits are unset in the given range or not
- Check if all the set bits of the binary representation of N are at least K places away
- Check if bits in range L to R of two numbers are complement of each other or not
- Check whether bits are in alternate pattern in the given range | Set-2
Improved By : Smitha Dinesh Semwal