Check if a number has two adjacent set bits - GeeksforGeeks

03 May, 2021
Given a number you have to check whether there is pair of adjacent set bit or not.Examples :  
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 to 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.  
C++
Java
Python3
C#
php
Javascript
// CPP program to check// if there are two// adjacent set bits.#include <iostream>using namespace std; bool adjacentSet(int n){    return (n & (n >> 1));} // Driver Codeint main(){    int n = 3;    adjacentSet(n) ?     cout << "Yes" :       cout << "No";    return 0;}
// Java program to check// if there are two// adjacent set bits.class GFG{         static boolean adjacentSet(int n)    {        int x = (n & (n >> 1));                 if(x > 0)            return true;        else            return false;    }         // Driver code    public static void main(String args[])    {         int n = 3;                 if(adjacentSet(n))            System.out.println("Yes");        else            System.out.println("No");     }} // This code is contributed by Sam007.
# Python 3 program to check if there# are two adjacent set bits. def adjacentSet(n):    return (n & (n >> 1)) # Driver Codeif __name__ == '__main__':    n = 3    if (adjacentSet(n)):        print("Yes")    else:        print("No")         # This code is contributed by# Shashank_Sharma
// C# program to check// if there are two// adjacent set bits.using System; class GFG{    static bool adjacentSet(int n)    {        int x = (n & (n >> 1));                 if(x > 0)            return true;        else            return false;    }         // Driver code    public static void Main ()    {        int n = 3;                 if(adjacentSet(n))            Console.WriteLine("Yes");        else            Console.WriteLine("No");    }         } // This code is contributed by Sam007.
<?php// PHP program to check// if there are two// adjacent set bits. function adjacentSet($n){    return ($n & ($n >> 1));} // Driver Code$n = 3;adjacentSet($n) ?   print("Yes") :     print("No"); // This code is contributed by Sam007.?>
<script> // Javascript program to check// if there are two// adjacent set bits.  function adjacentSet(n)    {        let x = (n & (n >> 1));                 if(x > 0)            return true;        else            return false;    } // driver program            let n = 3;                 if(adjacentSet(n))            document.write("Yes");        else            document.write("No");         </script>
Output :  
Yes
 
YouTubeGeeksforGeeks500K subscribersCheck if a number has two adjacent set bits | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:28•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=vCBaiGkDItM" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Pranav. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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. 
Sam007
Shashank_Sharma
ashwin1
sanjoy_62
Numbers
Bit Magic
Bit Magic
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery
Add two numbers without using arithmetic operators
Find the element that appears once
Set, Clear and Toggle a given bit of a number in C
Bit Fields in C
Bits manipulation (Important tactics)
Josephus problem | Set 1 (A O(n) Solution)
Write an Efficient C Program to Reverse Bits of a Number
C++ bitset and its application