Skip to main content

Check if a number has two adjacent active bits

Check if two adjacent bits are set in a number

Here given code implementation process.

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

// Check that two adjacent set bits exist in given number
void adjacentActiveBit(int num)
{
	// 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
		printf("\n Adjacent active bit exist\n");
	}
	else
	{
		printf("\n Adjacent active bit not exist\n");
	}
}
int main(int argc, char
	const *argv[])
{
	// Test Cases
	// 12 (1100)
	adjacentActiveBit(12);
	// 34 (100010)
	adjacentActiveBit(34);
	// -12 (∞11110100)
	adjacentActiveBit(-12);
	// 7 (111)
	adjacentActiveBit(7);
	// 21 (00010101)
	adjacentActiveBit(21);
	return 0;
}

Output

 Number : 12
 Adjacent active bit exist

 Number : 34
 Adjacent active bit not exist

 Number : -12
 Adjacent active bit exist

 Number : 7
 Adjacent active bit exist

 Number : 21
 Adjacent active bit not exist
/*
  Java Program 
  Check if a number has two adjacent active bits
*/
public class BitPosition
{
	// Check that two adjacent set bits exist in given number
	public void adjacentActiveBit(int num)
	{
		// 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
			System.out.print("\n Adjacent active bit exist\n");
		}
		else
		{
			System.out.print("\n Adjacent active bit not exist\n");
		}
	}
	public static void main(String[] args)
	{
		BitPosition task = new BitPosition();
		// Test Cases
		// 12 (1100)
		task.adjacentActiveBit(12);
		// 34 (100010)
		task.adjacentActiveBit(34);
		// -12 (∞11110100)
		task.adjacentActiveBit(-12);
		// 7 (111)
		task.adjacentActiveBit(7);
		// 21 (00010101)
		task.adjacentActiveBit(21);
	}
}

Output

 Number : 12
 Adjacent active bit exist

 Number : 34
 Adjacent active bit not exist

 Number : -12
 Adjacent active bit exist

 Number : 7
 Adjacent active bit exist

 Number : 21
 Adjacent active bit not exist
// 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
		void adjacentActiveBit(int num)
		{
			// 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()
{
	BitPosition task = BitPosition();
	// Test Cases
	// 12 (1100)
	task.adjacentActiveBit(12);
	// 34 (100010)
	task.adjacentActiveBit(34);
	// -12 (∞11110100)
	task.adjacentActiveBit(-12);
	// 7 (111)
	task.adjacentActiveBit(7);
	// 21 (00010101)
	task.adjacentActiveBit(21);
	return 0;
}

Output

 Number : 12
 Adjacent active bit exist

 Number : 34
 Adjacent active bit not exist

 Number : -12
 Adjacent active bit exist

 Number : 7
 Adjacent active bit exist

 Number : 21
 Adjacent active bit not exist
// 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
	public void adjacentActiveBit(int num)
	{
		// 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
			Console.Write("\n Adjacent active bit exist\n");
		}
		else
		{
			Console.Write("\n Adjacent active bit not exist\n");
		}
	}
	public static void Main(String[] args)
	{
		BitPosition task = new BitPosition();
		// Test Cases
		// 12 (1100)
		task.adjacentActiveBit(12);
		// 34 (100010)
		task.adjacentActiveBit(34);
		// -12 (∞11110100)
		task.adjacentActiveBit(-12);
		// 7 (111)
		task.adjacentActiveBit(7);
		// 21 (00010101)
		task.adjacentActiveBit(21);
	}
}

Output

 Number : 12
 Adjacent active bit exist

 Number : 34
 Adjacent active bit not exist

 Number : -12
 Adjacent active bit exist

 Number : 7
 Adjacent active bit exist

 Number : 21
 Adjacent active bit not exist
<?php
/*
  Php Program 
  Check if a number has two adjacent active bits
*/
class BitPosition
{
	// Check that two adjacent set bits exist in given number
	public	function adjacentActiveBit($num)
	{
		// 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()
{
	$task = new BitPosition();
	// Test Cases
	// 12 (1100)
	$task->adjacentActiveBit(12);
	// 34 (100010)
	$task->adjacentActiveBit(34);
	// -12 (∞11110100)
	$task->adjacentActiveBit(-12);
	// 7 (111)
	$task->adjacentActiveBit(7);
	// 21 (00010101)
	$task->adjacentActiveBit(21);
}
main();

Output

 Number : 12
 Adjacent active bit exist

 Number : 34
 Adjacent active bit not exist

 Number : -12
 Adjacent active bit exist

 Number : 7
 Adjacent active bit exist

 Number : 21
 Adjacent active bit not exist
/*
  Node Js Program 
  Check if a number has two adjacent active bits
*/
class BitPosition
{
	// Check that two adjacent set bits exist in given number
	adjacentActiveBit(num)
	{
		// 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
			process.stdout.write("\n Adjacent active bit exist\n");
		}
		else
		{
			process.stdout.write("\n Adjacent active bit not exist\n");
		}
	}
}

function main()
{
	var task = new BitPosition();
	// Test Cases
	// 12 (1100)
	task.adjacentActiveBit(12);
	// 34 (100010)
	task.adjacentActiveBit(34);
	// -12 (∞11110100)
	task.adjacentActiveBit(-12);
	// 7 (111)
	task.adjacentActiveBit(7);
	// 21 (00010101)
	task.adjacentActiveBit(21);
}
main();

Output

 Number : 12
 Adjacent active bit exist

 Number : 34
 Adjacent active bit not exist

 Number : -12
 Adjacent active bit exist

 Number : 7
 Adjacent active bit exist

 Number : 21
 Adjacent active bit not exist
#   Python 3 Program 
#   Check if a number has two adjacent active bits

class BitPosition :
	#  Check that two adjacent set bits exist in given number
	def adjacentActiveBit(self, num) :
		#  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
			print("\n Adjacent active bit exist")
		else :
			print("\n Adjacent active bit not exist")
		
	

def main() :
	task = BitPosition()
	#  Test Cases
	#  12 (1100)
	task.adjacentActiveBit(12)
	#  34 (100010)
	task.adjacentActiveBit(34)
	#  -12 (∞11110100)
	task.adjacentActiveBit(-12)
	#  7 (111)
	task.adjacentActiveBit(7)
	#  21 (00010101)
	task.adjacentActiveBit(21)

if __name__ == "__main__": main()

Output

 Number :  12
 Adjacent active bit exist

 Number :  34
 Adjacent active bit not exist

 Number :  -12
 Adjacent active bit exist

 Number :  7
 Adjacent active bit exist

 Number :  21
 Adjacent active bit not exist
#   Ruby 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) 
		#  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
			print("\n Adjacent active bit exist\n")
		else 
			print("\n Adjacent active bit not exist\n")
		end

	end

end

def main() 
	task = BitPosition.new()
	#  Test Cases
	#  12 (1100)
	task.adjacentActiveBit(12)
	#  34 (100010)
	task.adjacentActiveBit(34)
	#  -12 (∞11110100)
	task.adjacentActiveBit(-12)
	#  7 (111)
	task.adjacentActiveBit(7)
	#  21 (00010101)
	task.adjacentActiveBit(21)
end

main()

Output

 Number : 12
 Adjacent active bit exist

 Number : 34
 Adjacent active bit not exist

 Number : -12
 Adjacent active bit exist

 Number : 7
 Adjacent active bit exist

 Number : 21
 Adjacent active bit not exist
/*
  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
			print("\n Adjacent active bit exist\n");
		}
		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)
		task.adjacentActiveBit(12);
		// 34 (100010)
		task.adjacentActiveBit(34);
		// -12 (∞11110100)
		task.adjacentActiveBit(-12);
		// 7 (111)
		task.adjacentActiveBit(7);
		// 21 (00010101)
		task.adjacentActiveBit(21);
	}
}

Output

 Number : 12
 Adjacent active bit exist

 Number : 34
 Adjacent active bit not exist

 Number : -12
 Adjacent active bit exist

 Number : 7
 Adjacent active bit exist

 Number : 21
 Adjacent active bit not exist
/*
  Swift 4 Program 
  Check if a number has two adjacent active bits
*/
class BitPosition
{
	// Check that two adjacent set bits exist in given number
	func adjacentActiveBit(_ num: Int)
	{
		// 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
			print("\n Adjacent active bit exist");
		}
		else
		{
			print("\n Adjacent active bit not exist");
		}
	}
}
func main()
{
	let task: BitPosition = BitPosition();
	// Test Cases
	// 12 (1100)
	task.adjacentActiveBit(12);
	// 34 (100010)
	task.adjacentActiveBit(34);
	// -12 (∞11110100)
	task.adjacentActiveBit(-12);
	// 7 (111)
	task.adjacentActiveBit(7);
	// 21 (00010101)
	task.adjacentActiveBit(21);
}
main();

Output

 Number :  12
 Adjacent active bit exist

 Number :  34
 Adjacent active bit not exist

 Number :  -12
 Adjacent active bit exist

 Number :  7
 Adjacent active bit exist

 Number :  21
 Adjacent active bit not exist
/*
  Kotlin Program 
  Check if a number has two adjacent active bits
*/
class BitPosition
{
	// Check that two adjacent set bits exist in given number
	fun 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 and(num shr 1)) > 0))
		{
			// If result are greater than zero
			// Or num less than zero
			print("\n Adjacent active bit exist\n");
		}
		else
		{
			print("\n Adjacent active bit not exist\n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: BitPosition = BitPosition();
	// Test Cases
	// 12 (1100)
	task.adjacentActiveBit(12);
	// 34 (100010)
	task.adjacentActiveBit(34);
	// -12 (∞11110100)
	task.adjacentActiveBit(-12);
	// 7 (111)
	task.adjacentActiveBit(7);
	// 21 (00010101)
	task.adjacentActiveBit(21);
}

Output

 Number : 12
 Adjacent active bit exist

 Number : 34
 Adjacent active bit not exist

 Number : -12
 Adjacent active bit exist

 Number : 7
 Adjacent active bit exist

 Number : 21
 Adjacent active bit not exist
// Rust Program 
// Check if a number has two adjacent active bits
fn main()
{
	// Test Cases
	// 12 (1100)
	adjacent_active_bit(12);
	// 34 (100010)
	adjacent_active_bit(34);
	// -12 (∞11110100)
	adjacent_active_bit(-12);
	// 7 (111)
	adjacent_active_bit(7);
	// 21 (00010101)
	adjacent_active_bit(21);
}
fn adjacent_active_bit(num: i32)
{
	// 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
		print!("\n Adjacent active bit exist\n");
	}
	else
	{
		print!("\n Adjacent active bit not exist\n");
	}
}

Output

 Number : 12
 Adjacent active bit exist

 Number : 34
 Adjacent active bit not exist

 Number : -12
 Adjacent active bit exist

 Number : 7
 Adjacent active bit exist

 Number : 21
 Adjacent active bit not exist




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.

New Comment