# Check if a number has two adjacent active bits

Determine whether a given integer has two adjacent active bits (1s) in its binary representation. In other words, we want to check if there are two consecutive 1s in the binary form of the number. This involves bitwise operations to analyze the binary representation and identify the presence of adjacent active bits. ## Problem Statement and Description

Given an integer `num`, the task is to determine whether there are two adjacent active bits (1s) in its binary representation. We need to perform bitwise operations to check if there is a consecutive occurrence of two 1s in the binary form of the number.

## Example

Consider the number 12. In binary form, it is represented as 1100. This number contains two adjacent active bits (1s) at positions 2 and 3. Therefore, the expected output for this example should be "Adjacent active bit exist".

1. For the number 12:

• Binary representation: 1100
• Two adjacent active bits exist at positions 2 and 3.
• The output is "Number: 12\nAdjacent active bit exist".
2. For the number 34:

• Binary representation: 100010
• No two adjacent active bits are present.
• The output is "Number: 34\nAdjacent active bit not exist".
3. For the number -12:

• Binary representation: (∞11110100)
• Two adjacent active bits exist at positions 2 and 3 (considering 32-bit representation).
• The output is "Number: -12\nAdjacent active bit exist".
4. For the number 7:

• Binary representation: 111
• Two adjacent active bits exist.
• The output is "Number: 7\nAdjacent active bit exist".
5. For the number 21:

• Binary representation: 10101
• No two adjacent active bits are present.
• The output is "Number: 21\nAdjacent active bit not exist".

## Idea to Solve the Problem

The key idea to solve this problem is to use bitwise operations to check if there are two adjacent active bits in the binary representation of the given number. We can shift the number to the right by one position and perform a bitwise AND operation with the original number. If the result is greater than zero, it means that two adjacent active bits exist. Additionally, if the given number is negative, it will have a sign bit that may affect the result, so we need to handle that case as well.

## Pseudocode

Here's the pseudocode for the algorithm:

``````function adjacentActiveBit(num):
print "Number: ", num
if num < 0 or (num & (num >> 1)) > 0:
else:
print "Adjacent active bit not exist"``````

## Algorithm Explanation

1. The function `adjacentActiveBit(num)` takes an integer `num` as input.
2. It prints the given number.
3. It checks if the given number is negative (`num < 0`) or if the result of `(num & (num >> 1))` is greater than zero. The expression `(num & (num >> 1))` checks for the presence of two adjacent active bits using bitwise operations.
4. If either of the conditions is met, it means that two adjacent active bits exist, and it prints "Adjacent active bit exist".
5. Otherwise, it prints "Adjacent active bit not exist".

## Code Solution

``````// C Program
// Check if a number has two adjacent active bits
#include <stdio.h>

// Check that two adjacent set bits exist in given number
{
// Display given number
printf("\n Number : %d", num);
// Shift the given number by one on left side and perform bitwise & operation
if (num < 0 || (num & (num >> 1)) > 0)
{
// If result are greater than zero
// Or num less than zero
}
else
{
printf("\n Adjacent active bit not exist\n");
}
}
int main(int argc, char
const *argv[])
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
return 0;
}``````

#### Output

`````` Number : 12

Number : 34

Number : -12

Number : 7

Number : 21
``````/*
Java Program
Check if a number has two adjacent active bits
*/
public class BitPosition
{
// Check that two adjacent set bits exist in given number
{
// Display given number
System.out.print("\n Number : " + num);
// Shift the given number by one on left side and perform bitwise & operation
if (num < 0 || ((num & (num >> 1)) > 0))
{
// If result are greater than zero
// Or num less than zero
}
else
{
System.out.print("\n Adjacent active bit not exist\n");
}
}
public static void main(String[] args)
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
}
}``````

#### Output

`````` Number : 12

Number : 34

Number : -12

Number : 7

Number : 21
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Check if a number has two adjacent active bits
*/
class BitPosition
{
public:
// Check that two adjacent set bits exist in given number
{
// Display given number
cout << "\n Number : " << num;
// Shift the given number by one on left side and perform bitwise &operation
if (num < 0 || ((num &(num >> 1)) > 0))
{
// If result are greater than zero
// Or num less than zero
cout << "\n Adjacent active bit exist\n";
}
else
{
cout << "\n Adjacent active bit not exist\n";
}
}
};
int main()
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
return 0;
}``````

#### Output

`````` Number : 12

Number : 34

Number : -12

Number : 7

Number : 21
``````// Include namespace system
using System;
/*
C# Program
Check if a number has two adjacent active bits
*/
public class BitPosition
{
// Check that two adjacent set bits exist in given number
{
// Display given number
Console.Write("\n Number : " + num);
// Shift the given number by one on left side and perform bitwise & operation
if (num < 0 || ((num & (num >> 1)) > 0))
{
// If result are greater than zero
// Or num less than zero
}
else
{
Console.Write("\n Adjacent active bit not exist\n");
}
}
public static void Main(String[] args)
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
}
}``````

#### Output

`````` Number : 12

Number : 34

Number : -12

Number : 7

Number : 21
``````<?php
/*
Php Program
Check if a number has two adjacent active bits
*/
class BitPosition
{
// Check that two adjacent set bits exist in given number
{
// Display given number
echo "\n Number : ". \$num;
// Shift the given number by one on left side and perform bitwise & operation
if (\$num < 0 || ((\$num & (\$num >> 1)) > 0))
{
// If result are greater than zero
// Or num less than zero
echo "\n Adjacent active bit exist\n";
}
else
{
echo "\n Adjacent active bit not exist\n";
}
}
}

function main()
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
}
main();``````

#### Output

`````` Number : 12

Number : 34

Number : -12

Number : 7

Number : 21
``````/*
Node Js Program
Check if a number has two adjacent active bits
*/
class BitPosition
{
// Check that two adjacent set bits exist in given number
{
// Display given number
process.stdout.write("\n Number : " + num);
// Shift the given number by one on left side and perform bitwise & operation
if (num < 0 || ((num & (num >> 1)) > 0))
{
// If result are greater than zero
// Or num less than zero
}
else
{
process.stdout.write("\n Adjacent active bit not exist\n");
}
}
}

function main()
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
}
main();``````

#### Output

`````` Number : 12

Number : 34

Number : -12

Number : 7

Number : 21
``````#   Python 3 Program
#   Check if a number has two adjacent active bits

class BitPosition :
#  Check that two adjacent set bits exist in given number
#  Display given number
print("\n Number : ", num, end = "")
#  Shift the given number by one on left side and perform bitwise & operation
if (num < 0 or((num & (num >> 1)) > 0)) :
#  If result are greater than zero
#  Or num less than zero
else :
print("\n Adjacent active bit not exist")

def main() :
#  Test Cases
#  12 (1100)
#  34 (100010)
#  -12 (∞11110100)
#  7 (111)
#  21 (00010101)

if __name__ == "__main__": main()``````

#### Output

`````` Number :  12

Number :  34

Number :  -12

Number :  7

Number :  21
``````#   Ruby Program
#   Check if a number has two adjacent active bits

class BitPosition
#  Check that two adjacent set bits exist in given number
#  Display given number
print("\n Number : ", num)
#  Shift the given number by one on left side and perform bitwise & operation
if (num < 0 || ((num & (num >> 1)) > 0))
#  If result are greater than zero
#  Or num less than zero
else
print("\n Adjacent active bit not exist\n")
end

end

end

def main()
#  Test Cases
#  12 (1100)
#  34 (100010)
#  -12 (∞11110100)
#  7 (111)
#  21 (00010101)
end

main()``````

#### Output

`````` Number : 12

Number : 34

Number : -12

Number : 7

Number : 21
``````
``````/*
Scala Program
Check if a number has two adjacent active bits
*/
class BitPosition
{
// Check that two adjacent set bits exist in given number
def adjacentActiveBit(num: Int): Unit = {
// Display given number
print("\n Number : " + num);
// Shift the given number by one on left side and perform bitwise & operation
if (num < 0 || ((num & (num >> 1)) > 0))
{
// If result are greater than zero
// Or num less than zero
}
else
{
print("\n Adjacent active bit not exist\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: BitPosition = new BitPosition();
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
}
}``````

#### Output

`````` Number : 12

Number : 34

Number : -12

Number : 7

Number : 21
``````/*
Swift 4 Program
Check if a number has two adjacent active bits
*/
class BitPosition
{
// Check that two adjacent set bits exist in given number
{
// Display given number
print("\n Number : ", num, terminator: "");
// Shift the given number by one on left side and perform bitwise & operation
if (num < 0 || ((num & (num >> 1)) > 0))
{
// If result are greater than zero
// Or num less than zero
}
else
{
print("\n Adjacent active bit not exist");
}
}
}
func main()
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
}
main();``````

#### Output

`````` Number :  12

Number :  34

Number :  -12

Number :  7

Number :  21
``````/*
Kotlin Program
Check if a number has two adjacent active bits
*/
class BitPosition
{
// Check that two adjacent set bits exist in given number
{
// Display given number
print("\n Number : " + num);
// Shift the given number by one on left side and perform bitwise & operation
if (num < 0 || ((num and(num shr 1)) > 0))
{
// If result are greater than zero
// Or num less than zero
}
else
{
print("\n Adjacent active bit not exist\n");
}
}
}
fun main(args: Array < String > ): Unit
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
}``````

#### Output

`````` Number : 12

Number : 34

Number : -12

Number : 7

Number : 21
``````// Rust Program
// Check if a number has two adjacent active bits
fn main()
{
// Test Cases
// 12 (1100)
// 34 (100010)
// -12 (∞11110100)
// 7 (111)
// 21 (00010101)
}
{
// Display given number
print!("\n Number : {}", num);
// Shift the given number by one on left side and perform bitwise & operation
if num < 0 || (num & (num >> 1)) > 0
{
// If result are greater than zero
// Or num less than zero
}
else
{
print!("\n Adjacent active bit not exist\n");
}
}``````

#### Output

`````` Number : 12

Number : 34

Number : -12

Number : 7

Number : 21

## Time Complexity

The time complexity of this algorithm is constant (O(1)) because the operations involved (bitwise AND, bitwise shift, comparisons) are all performed in constant time regardless of the input number.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.