Find position of all active bits in a number

Here given code implementation process.

// C Program 
// Find position of all active bits in a number
#include <stdio.h>

// Finding all active bit position of given positive number
void activeBitPosition(int n)
{
	if (n < 0)
	{
		return;
	}
	int i = 0;
	// Display given data
	printf("\n Number : %d", n);
	printf("\n Active Bit Position : ");
	int num = n;
	int status = 0;
	while (num > 0)
	{
		if (num & 1 == 1)
		{
			status = 1;
			printf(" %d ", i);
		}
		// Same as of shift left side by 1 (num >> 1)
		num /= 2;
		i++;
	}
	if (status == 0)
	{
		printf(" None \n");
	}
}
int main()
{
	// Test Case
	activeBitPosition(7);
	activeBitPosition(16);
	activeBitPosition(21);
	activeBitPosition(0);
	return 0;
}

Output

 Number : 7
 Active Bit Position :  0  1  2
 Number : 16
 Active Bit Position :  4
 Number : 21
 Active Bit Position :  0  2  4
 Number : 0
 Active Bit Position :  None
/*
   Java Program for
   Find position of all active bits in a number
*/
public class BitPosition
{
	// Finding all active bit position of given positive number
	public void activeBitPosition(int n)
	{
		if (n < 0)
		{
			return;
		}
		int i = 0;
		// Display given data
		System.out.print("\n Number : " + n );
		System.out.print("\n Active Bit Position : ");
		int num = n;
		boolean status = false;
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				status = true;
				System.out.print(" " + i + " ");
			}
			// Same as of shift left side by 1 (num >> 1)
			num /= 2;
			i++;
		}
		if (status == false)
		{
			System.out.print(" None \n");
		}
	}
	public static void main(String[] args)
	{
		BitPosition task = new BitPosition();
		// Test Case
		task.activeBitPosition(7);
		task.activeBitPosition(16);
		task.activeBitPosition(21);
		task.activeBitPosition(0);
	}
}

Output

 Number : 7
 Active Bit Position :  0  1  2
 Number : 16
 Active Bit Position :  4
 Number : 21
 Active Bit Position :  0  2  4
 Number : 0
 Active Bit Position :  None
// Include header file
#include <iostream>

using namespace std;
/*
   C++ Program for
   Find position of all active bits in a number
*/
class BitPosition
{
	public:
		// Finding all active bit position of given positive number
		void activeBitPosition(int n)
		{
			if (n < 0)
			{
				return;
			}
			int i = 0;
			// Display given data
			cout << "\n Number : " << n ;
			cout << "\n Active Bit Position : ";
			int num = n;
			bool status = false;
			while (num > 0)
			{
				if ((num &1) == 1)
				{
					status = true;
					cout << " " << i << " ";
				}
				// Same as of shift left side by 1 (num >> 1)
				num /= 2;
				i++;
			}
			if (status == false)
			{
				cout << " None \n";
			}
		}
};
int main()
{
	BitPosition task = BitPosition();
	// Test Case
	task.activeBitPosition(7);
	task.activeBitPosition(16);
	task.activeBitPosition(21);
	task.activeBitPosition(0);
	return 0;
}

Output

 Number : 7
 Active Bit Position :  0  1  2
 Number : 16
 Active Bit Position :  4
 Number : 21
 Active Bit Position :  0  2  4
 Number : 0
 Active Bit Position :  None
// Include namespace system
using System;
/*
   C# Program for
   Find position of all active bits in a number
*/
public class BitPosition
{
	// Finding all active bit position of given positive number
	public void activeBitPosition(int n)
	{
		if (n < 0)
		{
			return;
		}
		int i = 0;
		// Display given data
		Console.Write("\n Number : " + n );
		Console.Write("\n Active Bit Position : ");
		int num = n;
		Boolean status = false;
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				status = true;
				Console.Write(" " + i + " ");
			}
			// Same as of shift left side by 1 (num >> 1)
			num /= 2;
			i++;
		}
		if (status == false)
		{
			Console.Write(" None \n");
		}
	}
	public static void Main(String[] args)
	{
		BitPosition task = new BitPosition();
		// Test Case
		task.activeBitPosition(7);
		task.activeBitPosition(16);
		task.activeBitPosition(21);
		task.activeBitPosition(0);
	}
}

Output

 Number : 7
 Active Bit Position :  0  1  2
 Number : 16
 Active Bit Position :  4
 Number : 21
 Active Bit Position :  0  2  4
 Number : 0
 Active Bit Position :  None
<?php
/*
   Php Program for
   Find position of all active bits in a number
*/
class BitPosition
{
	// Finding all active bit position of given positive number
	public	function activeBitPosition($n)
	{
		if ($n < 0)
		{
			return;
		}
		$i = 0;
		// Display given data
		echo "\n Number : ". $n ;
		echo "\n Active Bit Position : ";
		$num = $n;
		$status = false;
		while ($num > 0)
		{
			if (($num & 1) == 1)
			{
				$status = true;
				echo " ". $i ." ";
			}
			// Same as of shift left side by 1 (num >> 1)
			$num = intval($num / 2);
			$i++;
		}
		if ($status == false)
		{
			echo " None \n";
		}
	}
}

function main()
{
	$task = new BitPosition();
	// Test Case
	$task->activeBitPosition(7);
	$task->activeBitPosition(16);
	$task->activeBitPosition(21);
	$task->activeBitPosition(0);
}
main();

Output

 Number : 7
 Active Bit Position :  0  1  2
 Number : 16
 Active Bit Position :  4
 Number : 21
 Active Bit Position :  0  2  4
 Number : 0
 Active Bit Position :  None
/*
   Node Js Program for
   Find position of all active bits in a number
*/
class BitPosition
{
	// Finding all active bit position of given positive number
	activeBitPosition(n)
	{
		if (n < 0)
		{
			return;
		}
		var i = 0;
		// Display given data
		process.stdout.write("\n Number : " + n );
		process.stdout.write("\n Active Bit Position : ");
		var num = n;
		var status = false;
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				status = true;
				process.stdout.write(" " + i + " ");
			}
			// Same as of shift left side by 1 (num >> 1)
			num = parseInt(num / 2);
			i++;
		}
		if (status == false)
		{
			process.stdout.write(" None \n");
		}
	}
}

function main()
{
	var task = new BitPosition();
	// Test Case
	task.activeBitPosition(7);
	task.activeBitPosition(16);
	task.activeBitPosition(21);
	task.activeBitPosition(0);
}
main();

Output

 Number : 7
 Active Bit Position :  0  1  2
 Number : 16
 Active Bit Position :  4
 Number : 21
 Active Bit Position :  0  2  4
 Number : 0
 Active Bit Position :  None
#    Python 3 Program for
#    Find position of all active bits in a number

class BitPosition :
	#  Finding all active bit position of given positive number
	def activeBitPosition(self, n) :
		if (n < 0) :
			return
		
		i = 0
		#  Display given data
		print("\n Number : ", n , end = "")
		print("\n Active Bit Position : ", end = "")
		num = n
		status = False
		while (num > 0) :
			if ((num & 1) == 1) :
				status = True
				print(" ", i ," ", end = "")
			
			num = int(num /
				#  Same as of shift left side by 1 (num >> 1)
				2)
			i += 1
		
		if (status == False) :
			print(" None ")
		
	

def main() :
	task = BitPosition()
	#  Test Case
	task.activeBitPosition(7)
	task.activeBitPosition(16)
	task.activeBitPosition(21)
	task.activeBitPosition(0)

if __name__ == "__main__": main()

Output

 Number :  7
 Active Bit Position :   0    1    2
 Number :  16
 Active Bit Position :   4
 Number :  21
 Active Bit Position :   0    2    4
 Number :  0
 Active Bit Position :  None
#    Ruby Program for
#    Find position of all active bits in a number

class BitPosition 
	#  Finding all active bit position of given positive number
	def activeBitPosition(n) 
		if (n < 0) 
			return
		end

		i = 0
		#  Display given data
		print("\n Number : ", n )
		print("\n Active Bit Position : ")
		num = n
		status = false
		while (num > 0) 
			if ((num & 1) == 1) 
				status = true
				print(" ", i ," ")
			end

			#  Same as of shift left side by 1 (num >> 1)
			num /= 2
			i += 1
		end

		if (status == false) 
			print(" None \n")
		end

	end

end

def main() 
	task = BitPosition.new()
	#  Test Case
	task.activeBitPosition(7)
	task.activeBitPosition(16)
	task.activeBitPosition(21)
	task.activeBitPosition(0)
end

main()

Output

 Number : 7
 Active Bit Position :  0  1  2 
 Number : 16
 Active Bit Position :  4 
 Number : 21
 Active Bit Position :  0  2  4 
 Number : 0
 Active Bit Position :  None 
/*
   Scala Program for
   Find position of all active bits in a number
*/
class BitPosition
{
	// Finding all active bit position of given positive number
	def activeBitPosition(n: Int): Unit = {
		if (n < 0)
		{
			return;
		}
		var i: Int = 0;
		// Display given data
		print("\n Number : " + n);
		print("\n Active Bit Position : ");
		var num: Int = n;
		var status: Boolean = false;
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				status = true;
				print(" " + i + " ");
			}
			// Same as of shift left side by 1 (num >> 1)
			num = (num / 2).toInt;
			i += 1;
		}
		if (status == false)
		{
			print(" None \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitPosition = new BitPosition();
		// Test Case
		task.activeBitPosition(7);
		task.activeBitPosition(16);
		task.activeBitPosition(21);
		task.activeBitPosition(0);
	}
}

Output

 Number : 7
 Active Bit Position :  0  1  2
 Number : 16
 Active Bit Position :  4
 Number : 21
 Active Bit Position :  0  2  4
 Number : 0
 Active Bit Position :  None
/*
   Swift 4 Program for
   Find position of all active bits in a number
*/
class BitPosition
{
	// Finding all active bit position of given positive number
	func activeBitPosition(_ n: Int)
	{
		if (n < 0)
		{
			return;
		}
		var i: Int = 0;
		// Display given data
		print("\n Number : ", n, terminator: "");
		print("\n Active Bit Position : ", terminator: "");
		var num: Int = n;
		var status: Bool = false;
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				status = true;
				print(" ", i ," ", terminator: "");
			}
			// Same as of shift left side by 1 (num >> 1)
			num /= 2;
			i += 1;
		}
		if (status == false)
		{
			print(" None ");
		}
	}
}
func main()
{
	let task: BitPosition = BitPosition();
	// Test Case
	task.activeBitPosition(7);
	task.activeBitPosition(16);
	task.activeBitPosition(21);
	task.activeBitPosition(0);
}
main();

Output

 Number :  7
 Active Bit Position :   0    1    2
 Number :  16
 Active Bit Position :   4
 Number :  21
 Active Bit Position :   0    2    4
 Number :  0
 Active Bit Position :  None
/*
   Kotlin Program for
   Find position of all active bits in a number
*/
class BitPosition
{
	// Finding all active bit position of given positive number
	fun activeBitPosition(n: Int): Unit
	{
		if (n < 0)
		{
			return;
		}
		var i: Int = 0;
		// Display given data
		print("\n Number : " + n);
		print("\n Active Bit Position : ");
		var num: Int = n;
		var status: Boolean = false;
		while (num > 0)
		{
			if ((num and 1) == 1)
			{
				status = true;
				print(" " + i + " ");
			}
			// Same as of shift left side by 1 (num >> 1)
			num /= 2;
			i += 1;
		}
		if (status == false)
		{
			print(" None \n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: BitPosition = BitPosition();
	// Test Case
	task.activeBitPosition(7);
	task.activeBitPosition(16);
	task.activeBitPosition(21);
	task.activeBitPosition(0);
}

Output

 Number : 7
 Active Bit Position :  0  1  2
 Number : 16
 Active Bit Position :  4
 Number : 21
 Active Bit Position :  0  2  4
 Number : 0
 Active Bit Position :  None

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