Skip to main content

Find position of k-th active bits in a number

Given a binary number, we can count the number of active (i.e., set to 1) bits in the number. The problem of finding the position of the k-th active bit in the number is to determine the index of the k-th 1 bit in the binary representation of the number. For example, if the binary number is 10011001, and we want to find the position of the 3rd active bit (i.e., the 3rd 1 bit), the answer would be 5.

Finding position of K-th set bit of number

Code Solution

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

// Find kth active bit position 
void activeBitPosition(int n, int k)
{
	if (n < 0 || k <= 0)
	{
		return;
	}
	// Display given data
	printf("\n Number : %d", n);
	printf("\n %d-th Active Bit Position is : ", k);
	int num = n;
	int i = 0;
	int counter = 0;
	while (num > 0 && counter < k)
	{
		if (num & 1 == 1)
		{
			counter++;
			if (counter == k)
			{
				//  When we get kth active element
				printf(" %d ", i);
			}
		}
		// Same as of shift left side by 1 (num >> 1)
		num /= 2;
		// Next position
		i++;
	}
	if (counter != k)
	{
		// When the kth active bit is not present
		printf(" None \n");
	}
}
int main()
{
	// Test Case
	// 25 = (11001) k = 1
	activeBitPosition(25, 1);
	// (1000) = (1111101000) k = 3
	activeBitPosition(1000, 3);
	// (153) = (10011001)  k = 2
	activeBitPosition(153, 2);
	// (354) = (101100010)  k = 4
	// Position 8
	activeBitPosition(354, 4);
	// 25 = (11001) k = 5
	activeBitPosition(25, 5);
	return 0;
}

Output

 Number : 25
 1-th Active Bit Position is :  0
 Number : 1000
 3-th Active Bit Position is :  6
 Number : 153
 2-th Active Bit Position is :  3
 Number : 354
 4-th Active Bit Position is :  8
 Number : 25
 5-th Active Bit Position is :  None
/*
   Java Program for
   Find position of k-th active bits in a number
*/
class BitPosition
{
	// Find kth active bit position 
	public void activeBitPosition(int n, int k)
	{
		if (n < 0 || k <= 0)
		{
			return;
		}
		// Display given data
		System.out.print("\n Number : " + n);
		System.out.print("\n " + k + "-th Active Bit Position is :");
		int num = n;
		int i = 0;
		int counter = 0;
		while (num > 0 && counter < k)
		{
			if ((num & 1) == 1)
			{
				counter++;
				if (counter == k)
				{
					//  When we get kth active element
					System.out.print("  " + i);
				}
			}
			// Same as of shift left side by 1 (num >> 1)
			num /= 2;
			// Next position
			i++;
		}
		if (counter != k)
		{
			// When the kth active bit is not present
			System.out.print(" None \n");
		}
	}
	public static void main(String[] args)
	{
		BitPosition task = new BitPosition();
		// Test Case
		// 25 = (11001) k = 1
		task.activeBitPosition(25, 1);
		// (1000) = (1111101000) k = 3
		task.activeBitPosition(1000, 3);
		// (153) = (10011001)  k = 2
		task.activeBitPosition(153, 2);
		// (354) = (101100010)  k = 4
		// Position 8
		task.activeBitPosition(354, 4);
		// 25 = (11001) k = 5
		task.activeBitPosition(25, 5);
	}
}

Output

 Number : 25
 1-th Active Bit Position is :  0
 Number : 1000
 3-th Active Bit Position is :  6
 Number : 153
 2-th Active Bit Position is :  3
 Number : 354
 4-th Active Bit Position is :  8
 Number : 25
 5-th Active Bit Position is : None
// Include header file
#include <iostream>

using namespace std;
/*
   C++ Program for
   Find position of k-th active bits in a number
*/
class BitPosition
{
	public:
		// Find kth active bit position
		void activeBitPosition(int n, int k)
		{
			if (n < 0 || k <= 0)
			{
				return;
			}
			// Display given data
			cout << "\n Number : " << n;
			cout << "\n " << k << "-th Active Bit Position is :";
			int num = n;
			int i = 0;
			int counter = 0;
			while (num > 0 && counter < k)
			{
				// Next position
				if ((num &1) == 1)
				{
					counter++;
					if (counter == k)
					{
						//  When we get kth active element
						cout << "  " << i;
					}
				}
				// Same as of shift left side by 1 (num >> 1)
				num /= 2;
				i++;
			}
			if (counter != k)
			{
				// When the kth active bit is not present
				cout << " None \n";
			}
		}
};
int main()
{
	BitPosition task = BitPosition();
	// Test Case
	// 25 = (11001) k = 1
	task.activeBitPosition(25, 1);
	// (1000) = (1111101000) k = 3
	task.activeBitPosition(1000, 3);
	// (153) = (10011001)  k = 2
	task.activeBitPosition(153, 2);
	// (354) = (101100010)  k = 4
	// Position 8
	task.activeBitPosition(354, 4);
	// 25 = (11001) k = 5
	task.activeBitPosition(25, 5);
	return 0;
}

Output

 Number : 25
 1-th Active Bit Position is :  0
 Number : 1000
 3-th Active Bit Position is :  6
 Number : 153
 2-th Active Bit Position is :  3
 Number : 354
 4-th Active Bit Position is :  8
 Number : 25
 5-th Active Bit Position is : None
// Include namespace system
using System;
/*
   C# Program for
   Find position of k-th active bits in a number
*/
public class BitPosition
{
	// Find kth active bit position
	public void activeBitPosition(int n, int k)
	{
		if (n < 0 || k <= 0)
		{
			return;
		}
		// Display given data
		Console.Write("\n Number : " + n);
		Console.Write("\n " + k + "-th Active Bit Position is :");
		int num = n;
		int i = 0;
		int counter = 0;
		while (num > 0 && counter < k)
		{
			// Next position
			if ((num & 1) == 1)
			{
				counter++;
				if (counter == k)
				{
					//  When we get kth active element
					Console.Write("  " + i);
				}
			}
			// Same as of shift left side by 1 (num >> 1)
			num /= 2;
			i++;
		}
		if (counter != k)
		{
			// When the kth active bit is not present
			Console.Write(" None \n");
		}
	}
	public static void Main(String[] args)
	{
		BitPosition task = new BitPosition();
		// Test Case
		// 25 = (11001) k = 1
		task.activeBitPosition(25, 1);
		// (1000) = (1111101000) k = 3
		task.activeBitPosition(1000, 3);
		// (153) = (10011001)  k = 2
		task.activeBitPosition(153, 2);
		// (354) = (101100010)  k = 4
		// Position 8
		task.activeBitPosition(354, 4);
		// 25 = (11001) k = 5
		task.activeBitPosition(25, 5);
	}
}

Output

 Number : 25
 1-th Active Bit Position is :  0
 Number : 1000
 3-th Active Bit Position is :  6
 Number : 153
 2-th Active Bit Position is :  3
 Number : 354
 4-th Active Bit Position is :  8
 Number : 25
 5-th Active Bit Position is : None
<?php
/*
   Php Program for
   Find position of k-th active bits in a number
*/
class BitPosition
{
	// Find kth active bit position
	public	function activeBitPosition($n, $k)
	{
		if ($n < 0 || $k <= 0)
		{
			return;
		}
		// Display given data
		echo "\n Number : ". $n;
		echo "\n ". $k ."-th Active Bit Position is :";
		$num = $n;
		$i = 0;
		$counter = 0;
		while ($num > 0 && $counter < $k)
		{
			// Next position
			if (($num & 1) == 1)
			{
				$counter++;
				if ($counter == $k)
				{
					//  When we get kth active element
					echo "  ". $i;
				}
			}
			// Same as of shift left side by 1 (num >> 1)
			$num = intval($num / 2);
			$i++;
		}
		if ($counter != $k)
		{
			// When the kth active bit is not present
			echo " None \n";
		}
	}
}

function main()
{
	$task = new BitPosition();
	// Test Case
	// 25 = (11001) k = 1
	$task->activeBitPosition(25, 1);
	// (1000) = (1111101000) k = 3
	$task->activeBitPosition(1000, 3);
	// (153) = (10011001)  k = 2
	$task->activeBitPosition(153, 2);
	// (354) = (101100010)  k = 4
	// Position 8
	$task->activeBitPosition(354, 4);
	// 25 = (11001) k = 5
	$task->activeBitPosition(25, 5);
}
main();

Output

 Number : 25
 1-th Active Bit Position is :  0
 Number : 1000
 3-th Active Bit Position is :  6
 Number : 153
 2-th Active Bit Position is :  3
 Number : 354
 4-th Active Bit Position is :  8
 Number : 25
 5-th Active Bit Position is : None
/*
   Node Js Program for
   Find position of k-th active bits in a number
*/
class BitPosition
{
	// Find kth active bit position
	activeBitPosition(n, k)
	{
		if (n < 0 || k <= 0)
		{
			return;
		}
		// Display given data
		process.stdout.write("\n Number : " + n);
		process.stdout.write("\n " + k + "-th Active Bit Position is :");
		var num = n;
		var i = 0;
		var counter = 0;
		while (num > 0 && counter < k)
		{
			// Next position
			if ((num & 1) == 1)
			{
				counter++;
				if (counter == k)
				{
					//  When we get kth active element
					process.stdout.write("  " + i);
				}
			}
			// Same as of shift left side by 1 (num >> 1)
			num = parseInt(num / 2);
			i++;
		}
		if (counter != k)
		{
			// When the kth active bit is not present
			process.stdout.write(" None \n");
		}
	}
}

function main()
{
	var task = new BitPosition();
	// Test Case
	// 25 = (11001) k = 1
	task.activeBitPosition(25, 1);
	// (1000) = (1111101000) k = 3
	task.activeBitPosition(1000, 3);
	// (153) = (10011001)  k = 2
	task.activeBitPosition(153, 2);
	// (354) = (101100010)  k = 4
	// Position 8
	task.activeBitPosition(354, 4);
	// 25 = (11001) k = 5
	task.activeBitPosition(25, 5);
}
main();

Output

 Number : 25
 1-th Active Bit Position is :  0
 Number : 1000
 3-th Active Bit Position is :  6
 Number : 153
 2-th Active Bit Position is :  3
 Number : 354
 4-th Active Bit Position is :  8
 Number : 25
 5-th Active Bit Position is : None
#    Python 3 Program for
#    Find position of k-th active bits in a number

class BitPosition :
	#  Find kth active bit position 
	def activeBitPosition(self, n, k) :
		if (n < 0 or k <= 0) :
			return
		
		#  Display given data
		print("\n Number : ", n, end = "")
		print("\n ", k ,"-th Active Bit Position is :", end = "")
		num = n
		i = 0
		counter = 0
		while (num > 0 and counter < k) :
			if ((num & 1) == 1) :
				counter += 1
				if (counter == k) :
					#   When we get kth active element
					print("  ", i, end = "")
				
			
			num = int(num /
				#  Same as of shift left side by 1 (num >> 1)
				2)
			#  Next position
			i += 1
		
		if (counter != k) :
			#  When the kth active bit is not present
			print(" None ")
		
	

def main() :
	task = BitPosition()
	#  Test Case
	#  25 = (11001) k = 1
	task.activeBitPosition(25, 1)
	#  (1000) = (1111101000) k = 3
	task.activeBitPosition(1000, 3)
	#  (153) = (10011001)  k = 2
	task.activeBitPosition(153, 2)
	#  (354) = (101100010)  k = 4
	#  Position 8
	task.activeBitPosition(354, 4)
	#  25 = (11001) k = 5
	task.activeBitPosition(25, 5)

if __name__ == "__main__": main()

Output

 Number :  25
  1 -th Active Bit Position is :   0
 Number :  1000
  3 -th Active Bit Position is :   6
 Number :  153
  2 -th Active Bit Position is :   3
 Number :  354
  4 -th Active Bit Position is :   8
 Number :  25
  5 -th Active Bit Position is : None
#    Ruby Program for
#    Find position of k-th active bits in a number

class BitPosition 
	#  Find kth active bit position 
	def activeBitPosition(n, k) 
		if (n < 0 || k <= 0) 
			return
		end

		#  Display given data
		print("\n Number : ", n)
		print("\n ", k ,"-th Active Bit Position is :")
		num = n
		i = 0
		counter = 0
		while (num > 0 && counter < k) 
			if ((num & 1) == 1) 
				counter += 1
				if (counter == k) 
					#   When we get kth active element
					print("  ", i)
				end

			end

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

		if (counter != k) 
			#  When the kth active bit is not present
			print(" None \n")
		end

	end

end

def main() 
	task = BitPosition.new()
	#  Test Case
	#  25 = (11001) k = 1
	task.activeBitPosition(25, 1)
	#  (1000) = (1111101000) k = 3
	task.activeBitPosition(1000, 3)
	#  (153) = (10011001)  k = 2
	task.activeBitPosition(153, 2)
	#  (354) = (101100010)  k = 4
	#  Position 8
	task.activeBitPosition(354, 4)
	#  25 = (11001) k = 5
	task.activeBitPosition(25, 5)
end

main()

Output

 Number : 25
 1-th Active Bit Position is :  0
 Number : 1000
 3-th Active Bit Position is :  6
 Number : 153
 2-th Active Bit Position is :  3
 Number : 354
 4-th Active Bit Position is :  8
 Number : 25
 5-th Active Bit Position is : None 
/*
   Scala Program for
   Find position of k-th active bits in a number
*/
class BitPosition
{
	// Find kth active bit position
	def activeBitPosition(n: Int, k: Int): Unit = {
		if (n < 0 || k <= 0)
		{
			return;
		}
		// Display given data
		print("\n Number : " + n);
		print("\n " + k + "-th Active Bit Position is :");
		var num: Int = n;
		var i: Int = 0;
		var counter: Int = 0;
		while (num > 0 && counter < k)
		{
			// Next position
			if ((num & 1) == 1)
			{
				counter += 1;
				if (counter == k)
				{
					//  When we get kth active element
					print("  " + i);
				}
			}
			// Same as of shift left side by 1 (num >> 1)
			num = (num / 2).toInt;
			i += 1;
		}
		if (counter != k)
		{
			// When the kth active bit is not present
			print(" None \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitPosition = new BitPosition();
		// Test Case
		// 25 = (11001) k = 1
		task.activeBitPosition(25, 1);
		// (1000) = (1111101000) k = 3
		task.activeBitPosition(1000, 3);
		// (153) = (10011001)  k = 2
		task.activeBitPosition(153, 2);
		// (354) = (101100010)  k = 4
		// Position 8
		task.activeBitPosition(354, 4);
		// 25 = (11001) k = 5
		task.activeBitPosition(25, 5);
	}
}

Output

 Number : 25
 1-th Active Bit Position is :  0
 Number : 1000
 3-th Active Bit Position is :  6
 Number : 153
 2-th Active Bit Position is :  3
 Number : 354
 4-th Active Bit Position is :  8
 Number : 25
 5-th Active Bit Position is : None
/*
   Swift 4 Program for
   Find position of k-th active bits in a number
*/
class BitPosition
{
	// Find kth active bit position
	func activeBitPosition(_ n: Int, _ k: Int)
	{
		if (n < 0 || k <= 0)
		{
			return;
		}
		// Display given data
		print("\n Number : ", n, terminator: "");
		print("\n ", k ,"-th Active Bit Position is :", terminator: "");
		var num: Int = n;
		var i: Int = 0;
		var counter: Int = 0;
		while (num > 0 && counter < k)
		{
			// Next position
			if ((num & 1) == 1)
			{
				counter += 1;
				if (counter == k)
				{
					//  When we get kth active element
					print("  ", i, terminator: "");
				}
			}
			// Same as of shift left side by 1 (num >> 1)
			num /= 2;
			i += 1;
		}
		if (counter  != k)
		{
			// When the kth active bit is not present
			print(" None ");
		}
	}
}
func main()
{
	let task: BitPosition = BitPosition();
	// Test Case
	// 25 = (11001) k = 1
	task.activeBitPosition(25, 1);
	// (1000) = (1111101000) k = 3
	task.activeBitPosition(1000, 3);
	// (153) = (10011001)  k = 2
	task.activeBitPosition(153, 2);
	// (354) = (101100010)  k = 4
	// Position 8
	task.activeBitPosition(354, 4);
	// 25 = (11001) k = 5
	task.activeBitPosition(25, 5);
}
main();

Output

 Number :  25
  1 -th Active Bit Position is :   0
 Number :  1000
  3 -th Active Bit Position is :   6
 Number :  153
  2 -th Active Bit Position is :   3
 Number :  354
  4 -th Active Bit Position is :   8
 Number :  25
  5 -th Active Bit Position is : None
/*
   Kotlin Program for
   Find position of k-th active bits in a number
*/
class BitPosition
{
	// Find kth active bit position
	fun activeBitPosition(n: Int, k: Int): Unit
	{
		if (n < 0 || k <= 0)
		{
			return;
		}
		// Display given data
		print("\n Number : " + n);
		print("\n " + k + "-th Active Bit Position is :");
		var num: Int = n;
		var i: Int = 0;
		var counter: Int = 0;
		while (num > 0 && counter < k)
		{
			// Next position
			if ((num and 1) == 1)
			{
				counter += 1;
				if (counter == k)
				{
					//  When we get kth active element
					print("  " + i);
				}
			}
			// Same as of shift left side by 1 (num >> 1)
			num /= 2;
			i += 1;
		}
		if (counter != k)
		{
			// When the kth active bit is not present
			print(" None \n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: BitPosition = BitPosition();
	// Test Case
	// 25 = (11001) k = 1
	task.activeBitPosition(25, 1);
	// (1000) = (1111101000) k = 3
	task.activeBitPosition(1000, 3);
	// (153) = (10011001)  k = 2
	task.activeBitPosition(153, 2);
	// (354) = (101100010)  k = 4
	// Position 8
	task.activeBitPosition(354, 4);
	// 25 = (11001) k = 5
	task.activeBitPosition(25, 5);
}

Output

 Number : 25
 1-th Active Bit Position is :  0
 Number : 1000
 3-th Active Bit Position is :  6
 Number : 153
 2-th Active Bit Position is :  3
 Number : 354
 4-th Active Bit Position is :  8
 Number : 25
 5-th Active Bit Position is : None




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