Comprobar si un número tiene dos bits establecidos adyacentes

Dado un número, debe verificar si hay un par de bits de conjunto adyacentes o no.
Ejemplos: 
 

Input : N = 67
Output : Yes
There is a pair of adjacent set bit
The binary representation is 100011

Input : N = 5
Output : No

C++

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

<?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.
?>

Javascript

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

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *