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.
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 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 whether the number has only first and last bits set | Set 2
- Check if a number has same number of set and unset bits
- Check if a number has bits in alternate pattern | Set 1
- Check if a number has bits in alternate pattern | Set-2 O(1) Approach
- Check whether the number has only first and last bits set
- Check if all bits can be made same by flipping two consecutive bits
- Maximum Sum of Products of two arrays by toggling adjacent bits
- Count pairs in an array such that both elements has equal set bits
- Maximum number of 0s that can be flipped such that Array has no adjacent 1s
- Print numbers having first and last bits as the only set bits
- Toggle bits of a number except first and last bits
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Program to find the Nth natural number with exactly two bits set
- Check if all bits of a number are set
- Check if bits in range L to R of two numbers are complement of each other or not
- Check whether a number has exactly three distinct factors or not
- Check if a number has prime count of divisors
- Prime Number of Set Bits in Binary Representation | Set 1