Check if all bits of a number is active

Here given code implementation process.

// C Program 
// Check if all bits of a number is active
#include <stdio.h>

// Determine that given number contains all active bits or not
void isActiveBits(int num)
{
	printf("\n Number : %d", num);
	if (num != 0 && ((num + 1) & num) == 0)
	{
		// When all set bit exist in given number
		printf("\n Yes\n");
	}
	else
	{
		printf("\n No\n");
	}
}
int main()
{
	// (-1) Binary 0001 => 1110 (1's) => 1111 (2s)
	isActiveBits(-1);
	// 00000100 = > 11111011 (1s) => 11111100 (2s)
	isActiveBits(-4);
	// (15) 1111 => Yes
	isActiveBits(15);
	// (100) 1100100 
	isActiveBits(100);
	return 0;
}

Output

 Number : -1
 Yes

 Number : -4
 No

 Number : 15
 Yes

 Number : 100
 No
/*
  Java Program for
  Check if all bits of a number is active
*/
public class BitStatus
{
	// Determine that given number contains all active bits or not
	public void isActiveBits(int num)
	{
		System.out.print("\n Number : " + num);
		if (num != 0 && ((num + 1) & num) == 0)
		{
			// When all set bit exist in given number
			System.out.print("\n Yes\n");
		}
		else
		{
			System.out.print("\n No\n");
		}
	}
	public static void main(String[] args)
	{
		BitStatus task = new BitStatus();
		// (-1) Binary 0001 => 1110 (1's) => 1111 (2s)
		task.isActiveBits(-1);
		// 00000100 = > 11111011 (1s) => 11111100 (2s)
		task.isActiveBits(-4);
		// (15) 1111 => Yes
		task.isActiveBits(15);
		// (100) 1100100 
		task.isActiveBits(100);
	}
}

Output

 Number : -1
 Yes

 Number : -4
 No

 Number : 15
 Yes

 Number : 100
 No
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Check if all bits of a number is active
*/
class BitStatus
{
	public:
		// Determine that given number contains all active bits or not
		void isActiveBits(int num)
		{
			cout << "\n Number : " << num;
			if (num != 0 && ((num + 1) &num) == 0)
			{
				// When all set bit exist in given number
				cout << "\n Yes\n";
			}
			else
			{
				cout << "\n No\n";
			}
		}
};
int main()
{
	BitStatus task = BitStatus();
	// (-1) Binary 0001 => 1110 (1's) => 1111 (2s)
	task.isActiveBits(-1);
	// 00000100 = > 11111011 (1s) => 11111100 (2s)
	task.isActiveBits(-4);
	// (15) 1111 => Yes
	task.isActiveBits(15);
	// (100) 1100100
	task.isActiveBits(100);
	return 0;
}

Output

 Number : -1
 Yes

 Number : -4
 No

 Number : 15
 Yes

 Number : 100
 No
// Include namespace system
using System;
/*
  C# Program for
  Check if all bits of a number is active
*/
public class BitStatus
{
	// Determine that given number contains all active bits or not
	public void isActiveBits(int num)
	{
		Console.Write("\n Number : " + num);
		if (num != 0 && ((num + 1) & num) == 0)
		{
			// When all set bit exist in given number
			Console.Write("\n Yes\n");
		}
		else
		{
			Console.Write("\n No\n");
		}
	}
	public static void Main(String[] args)
	{
		BitStatus task = new BitStatus();
		// (-1) Binary 0001 => 1110 (1's) => 1111 (2s)
		task.isActiveBits(-1);
		// 00000100 = > 11111011 (1s) => 11111100 (2s)
		task.isActiveBits(-4);
		// (15) 1111 => Yes
		task.isActiveBits(15);
		// (100) 1100100
		task.isActiveBits(100);
	}
}

Output

 Number : -1
 Yes

 Number : -4
 No

 Number : 15
 Yes

 Number : 100
 No
<?php
/*
  Php Program for
  Check if all bits of a number is active
*/
class BitStatus
{
	// Determine that given number contains all active bits or not
	public	function isActiveBits($num)
	{
		echo "\n Number : ". $num;
		if ($num != 0 && (($num + 1) & $num) == 0)
		{
			// When all set bit exist in given number
			echo "\n Yes\n";
		}
		else
		{
			echo "\n No\n";
		}
	}
}

function main()
{
	$task = new BitStatus();
	// (-1) Binary 0001 => 1110 (1's) => 1111 (2s)
	$task->isActiveBits(-1);
	// 00000100 = > 11111011 (1s) => 11111100 (2s)
	$task->isActiveBits(-4);
	// (15) 1111 => Yes
	$task->isActiveBits(15);
	// (100) 1100100
	$task->isActiveBits(100);
}
main();

Output

 Number : -1
 Yes

 Number : -4
 No

 Number : 15
 Yes

 Number : 100
 No
/*
  Node Js Program for
  Check if all bits of a number is active
*/
class BitStatus
{
	// Determine that given number contains all active bits or not
	isActiveBits(num)
	{
		process.stdout.write("\n Number : " + num);
		if (num != 0 && ((num + 1) & num) == 0)
		{
			// When all set bit exist in given number
			process.stdout.write("\n Yes\n");
		}
		else
		{
			process.stdout.write("\n No\n");
		}
	}
}

function main()
{
	var task = new BitStatus();
	// (-1) Binary 0001 => 1110 (1's) => 1111 (2s)
	task.isActiveBits(-1);
	// 00000100 = > 11111011 (1s) => 11111100 (2s)
	task.isActiveBits(-4);
	// (15) 1111 => Yes
	task.isActiveBits(15);
	// (100) 1100100
	task.isActiveBits(100);
}
main();

Output

 Number : -1
 Yes

 Number : -4
 No

 Number : 15
 Yes

 Number : 100
 No
#   Python 3 Program for
#   Check if all bits of a number is active

class BitStatus :
	#  Determine that given number contains all active bits or not
	def isActiveBits(self, num) :
		print("\n Number : ", num, end = "")
		if (num != 0 and((num + 1) & num) == 0) :
			#  When all set bit exist in given number
			print("\n Yes")
		else :
			print("\n No")
		
	

def main() :
	task = BitStatus()
	#  (-1) Binary 0001 => 1110 (1's) => 1111 (2s)
	task.isActiveBits(-1)
	#  00000100 = > 11111011 (1s) => 11111100 (2s)
	task.isActiveBits(-4)
	#  (15) 1111 => Yes
	task.isActiveBits(15)
	#  (100) 1100100 
	task.isActiveBits(100)

if __name__ == "__main__": main()

Output

 Number :  -1
 Yes

 Number :  -4
 No

 Number :  15
 Yes

 Number :  100
 No
#   Ruby Program for
#   Check if all bits of a number is active

class BitStatus 
	#  Determine that given number contains all active bits or not
	def isActiveBits(num) 
		print("\n Number : ", num)
		if (num != 0 && ((num + 1) & num) == 0) 
			#  When all set bit exist in given number
			print("\n Yes\n")
		else 
			print("\n No\n")
		end

	end

end

def main() 
	task = BitStatus.new()
	#  (-1) Binary 0001 => 1110 (1's) => 1111 (2s)
	task.isActiveBits(-1)
	#  00000100 = > 11111011 (1s) => 11111100 (2s)
	task.isActiveBits(-4)
	#  (15) 1111 => Yes
	task.isActiveBits(15)
	#  (100) 1100100 
	task.isActiveBits(100)
end

main()

Output

 Number : -1
 Yes

 Number : -4
 No

 Number : 15
 Yes

 Number : 100
 No
/*
  Scala Program for
  Check if all bits of a number is active
*/
class BitStatus
{
	// Determine that given number contains all active bits or not
	def isActiveBits(num: Int): Unit = {
		print("\n Number : " + num);
		if (num != 0 && ((num + 1) & num) == 0)
		{
			// When all set bit exist in given number
			print("\n Yes\n");
		}
		else
		{
			print("\n No\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitStatus = new BitStatus();
		// (-1) Binary 0001 => 1110 (1's) => 1111 (2s)
		task.isActiveBits(-1);
		// 00000100 = > 11111011 (1s) => 11111100 (2s)
		task.isActiveBits(-4);
		// (15) 1111 => Yes
		task.isActiveBits(15);
		// (100) 1100100
		task.isActiveBits(100);
	}
}

Output

 Number : -1
 Yes

 Number : -4
 No

 Number : 15
 Yes

 Number : 100
 No
/*
  Swift 4 Program for
  Check if all bits of a number is active
*/
class BitStatus
{
	// Determine that given number contains all active bits or not
	func isActiveBits(_ num: Int)
	{
		print("\n Number : ", num, terminator: "");
		if (num  != 0 && ((num + 1) & num) == 0)
		{
			// When all set bit exist in given number
			print("\n Yes");
		}
		else
		{
			print("\n No");
		}
	}
}
func main()
{
	let task: BitStatus = BitStatus();
	// (-1) Binary 0001 => 1110 (1"s) => 1111 (2s)
	task.isActiveBits(-1);
	// 00000100 = > 11111011 (1s) => 11111100 (2s)
	task.isActiveBits(-4);
	// (15) 1111 => Yes
	task.isActiveBits(15);
	// (100) 1100100
	task.isActiveBits(100);
}
main();

Output

 Number :  -1
 Yes

 Number :  -4
 No

 Number :  15
 Yes

 Number :  100
 No
/*
  Kotlin Program for
  Check if all bits of a number is active
*/
class BitStatus
{
	// Determine that given number contains all active bits or not
	fun isActiveBits(num: Int): Unit
	{
		print("\n Number : " + num);
		if (num != 0 && ((num + 1) and num) == 0)
		{
			// When all set bit exist in given number
			print("\n Yes\n");
		}
		else
		{
			print("\n No\n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: BitStatus = BitStatus();
	// (-1) Binary 0001 => 1110 (1's) => 1111 (2s)
	task.isActiveBits(-1);
	// 00000100 = > 11111011 (1s) => 11111100 (2s)
	task.isActiveBits(-4);
	// (15) 1111 => Yes
	task.isActiveBits(15);
	// (100) 1100100
	task.isActiveBits(100);
}

Output

 Number : -1
 Yes

 Number : -4
 No

 Number : 15
 Yes

 Number : 100
 No

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







© 2021, kalkicode.com, All rights reserved