Given a number you have to check whether there is pair of adjacent set bit or not.
Input : N = 67 Output : Yes There is a pair of adjacent set bit The binary representation is 100011 Input : N = 5 Output : No
A simple solution is traverse all bits. For every set bit, check if next bit is also set.
An efficient solution is to shift number by 1 and then do bitwise AND. If bitwise AND is non-zero then there are two adjacent set bits. Else not.
This article is contributed by Pranav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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 whether the number has only first and last bits set
- Check if all bits of a number are set
- Check whether the number has only first and last bits set | Set 2
- 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
- Maximum Sum of Products of two arrays by toggling adjacent 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 whether bits are in alternate pattern in the given range
- Check whether bits are in alternate pattern in the given range | Set-2