# Check if a number has two adjacent set bits

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
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

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.

## C++

 `// CPP program to check  ` `// if there are two ` `// adjacent set bits. ` `#include ` `using` `namespace` `std; ` ` `  `bool` `adjacentSet(``int` `n) ` `{ ` `    ``return` `(n & (n >> 1)); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 3; ` `    ``adjacentSet(n) ?  ` `     ``cout << ``"Yes"` `:  ` `       ``cout << ``"No"``; ` `    ``return` `0; ` `} `

## Java

 `// 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. `

## Python3

 `# Python 3 program to check if there  ` `# are two adjacent set bits. ` ` `  `def` `adjacentSet(n): ` `    ``return` `(n & (n >> ``1``)) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``n ``=` `3` `    ``if` `(adjacentSet(n)): ` `        ``print``(``"Yes"``) ` `    ``else``: ` `        ``print``(``"No"``) ` `         `  `# This code is contributed by ` `# Shashank_Sharma `

## C#

 `// 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

 `> 1)); ` `} ` ` `  `// Driver Code ` `\$n` `= 3; ` `adjacentSet(``\$n``) ?  ` `   ``print``(``"Yes"``) :  ` `     ``print``(``"No"``); ` ` `  `// This code is contributed by Sam007. ` `?> `

Output :

```Yes
```

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 contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

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.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.