Find the kth least significant bit of a number

Here given code implementation process.

// C Program 
// Find the kth least significant bit of a number
#include <stdio.h>

// Find kth least significant bit
void leastSignificant(int n, int k)
{
	if (k <= 0)
	{
		return;
	}
	// Find kth bit
	int bit = ((1 << (k - 1))) & n;
	printf("\n Number : %d", n);
	if (bit != 0)
	{
		bit = 1;
	}
	// Display calculated result
	printf("\n (%d) Least significant Bit is : %i ", k, bit);
}
int main()
{
	// Test Cases
	// 35 = (100011)
	int n = 35;
	int k = 6;
	leastSignificant(n, k);
	k = 3;
	leastSignificant(n, k);
	k = 2;
	leastSignificant(n, k);
	return 0;
}

Output

 Number : 35
 (6) Least significant Bit is : 1
 Number : 35
 (3) Least significant Bit is : 0
 Number : 35
 (2) Least significant Bit is : 1
/*
  Java Program for
  Find the kth least significant bit of a number
*/
class BitPosition
{
	// Find kth least significant bit
	public void leastSignificant(int n, int k)
	{
		if (k <= 0)
		{
			return;
		}
		// Find kth bit
		int bit = ((1 << (k - 1))) & n;
		if (bit != 0)
		{
			bit = 1;
		}
      	System.out.print("\n Number : " + n);
		// Display calculated result
		System.out.print("\n (" + k + ") Least significant Bit is : " + bit);
	}
	public static void main(String[] args)
	{
		BitPosition task = new BitPosition();
		// 35 = (100011)
		int n = 35;
		int k = 6;
		task.leastSignificant(n, k);
		k = 3;
		task.leastSignificant(n, k);
		k = 2;
		task.leastSignificant(n, k);
	}
}

Output

 Number : 35
 (6) Least significant Bit is : 1
 Number : 35
 (3) Least significant Bit is : 0
 Number : 35
 (2) Least significant Bit is : 1
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Find the kth least significant bit of a number
*/
class BitPosition
{
	public:
		// Find kth least significant bit
		void leastSignificant(int n, int k)
		{
			if (k <= 0)
			{
				return;
			}
			// Find kth bit
			int bit = ((1 << (k - 1))) &n;
			if (bit != 0)
			{
				bit = 1;
			}
			cout << "\n Number : " << n;
			// Display calculated result
			cout << "\n (" << k << ") Least significant Bit is : " << bit;
		}
};
int main()
{
	BitPosition task = BitPosition();
	// 35 = (100011)
	int n = 35;
	int k = 6;
	task.leastSignificant(n, k);
	k = 3;
	task.leastSignificant(n, k);
	k = 2;
	task.leastSignificant(n, k);
	return 0;
}

Output

 Number : 35
 (6) Least significant Bit is : 1
 Number : 35
 (3) Least significant Bit is : 0
 Number : 35
 (2) Least significant Bit is : 1
// Include namespace system
using System;
/*
  C# Program for
  Find the kth least significant bit of a number
*/
public class BitPosition
{
	// Find kth least significant bit
	public void leastSignificant(int n, int k)
	{
		if (k <= 0)
		{
			return;
		}
		// Find kth bit
		int bit = ((1 << (k - 1))) & n;
		if (bit != 0)
		{
			bit = 1;
		}
		Console.Write("\n Number : " + n);
		// Display calculated result
		Console.Write("\n (" + k + ") Least significant Bit is : " + bit);
	}
	public static void Main(String[] args)
	{
		BitPosition task = new BitPosition();
		// 35 = (100011)
		int n = 35;
		int k = 6;
		task.leastSignificant(n, k);
		k = 3;
		task.leastSignificant(n, k);
		k = 2;
		task.leastSignificant(n, k);
	}
}

Output

 Number : 35
 (6) Least significant Bit is : 1
 Number : 35
 (3) Least significant Bit is : 0
 Number : 35
 (2) Least significant Bit is : 1
<?php
/*
  Php Program for
  Find the kth least significant bit of a number
*/
class BitPosition
{
	// Find kth least significant bit
	public	function leastSignificant($n, $k)
	{
		if ($k <= 0)
		{
			return;
		}
		// Find kth bit
		$bit = ((1 << ($k - 1))) & $n;
		if ($bit != 0)
		{
			$bit = 1;
		}
		echo "\n Number : ". $n;
		// Display calculated result
		echo "\n (". $k .") Least significant Bit is : ". $bit;
	}
}

function main()
{
	$task = new BitPosition();
	// 35 = (100011)
	$n = 35;
	$k = 6;
	$task->leastSignificant($n, $k);
	$k = 3;
	$task->leastSignificant($n, $k);
	$k = 2;
	$task->leastSignificant($n, $k);
}
main();

Output

 Number : 35
 (6) Least significant Bit is : 1
 Number : 35
 (3) Least significant Bit is : 0
 Number : 35
 (2) Least significant Bit is : 1
/*
  Node Js Program for
  Find the kth least significant bit of a number
*/
class BitPosition
{
	// Find kth least significant bit
	leastSignificant(n, k)
	{
		if (k <= 0)
		{
			return;
		}
		// Find kth bit
		var bit = ((1 << (k - 1))) & n;
		if (bit != 0)
		{
			bit = 1;
		}
		process.stdout.write("\n Number : " + n);
		// Display calculated result
		process.stdout.write("\n (" + k + ") Least significant Bit is : " + bit);
	}
}

function main()
{
	var task = new BitPosition();
	// 35 = (100011)
	var n = 35;
	var k = 6;
	task.leastSignificant(n, k);
	k = 3;
	task.leastSignificant(n, k);
	k = 2;
	task.leastSignificant(n, k);
}
main();

Output

 Number : 35
 (6) Least significant Bit is : 1
 Number : 35
 (3) Least significant Bit is : 0
 Number : 35
 (2) Least significant Bit is : 1
#   Python 3 Program for
#   Find the kth least significant bit of a number

class BitPosition :
	#  Find kth least significant bit
	def leastSignificant(self, n, k) :
		if (k <= 0) :
			return
		
		#  Find kth bit
		bit = ((1 << (k - 1))) & n
		if (bit != 0) :
			bit = 1
		
		print("\n Number : ", n, end = "")
		#  Display calculated result
		print("\n (", k ,") Least significant Bit is : ", bit, end = "")
	

def main() :
	task = BitPosition()
	#  35 = (100011)
	n = 35
	k = 6
	task.leastSignificant(n, k)
	k = 3
	task.leastSignificant(n, k)
	k = 2
	task.leastSignificant(n, k)

if __name__ == "__main__": main()

Output

 Number :  35
 ( 6 ) Least significant Bit is :  1
 Number :  35
 ( 3 ) Least significant Bit is :  0
 Number :  35
 ( 2 ) Least significant Bit is :  1
#   Ruby Program for
#   Find the kth least significant bit of a number

class BitPosition 
	#  Find kth least significant bit
	def leastSignificant(n, k) 
		if (k <= 0) 
			return
		end

		#  Find kth bit
		bit = ((1 << (k - 1))) & n
		if (bit != 0) 
			bit = 1
		end

		print("\n Number : ", n)
		#  Display calculated result
		print("\n (", k ,") Least significant Bit is : ", bit)
	end

end

def main() 
	task = BitPosition.new()
	#  35 = (100011)
	n = 35
	k = 6
	task.leastSignificant(n, k)
	k = 3
	task.leastSignificant(n, k)
	k = 2
	task.leastSignificant(n, k)
end

main()

Output

 Number : 35
 (6) Least significant Bit is : 1
 Number : 35
 (3) Least significant Bit is : 0
 Number : 35
 (2) Least significant Bit is : 1
/*
  Scala Program for
  Find the kth least significant bit of a number
*/
class BitPosition
{
	// Find kth least significant bit
	def leastSignificant(n: Int, k: Int): Unit = {
		if (k <= 0)
		{
			return;
		}
		// Find kth bit
		var bit: Int = ((1 << (k - 1))) & n;
		if (bit != 0)
		{
			bit = 1;
		}
		print("\n Number : " + n);
		// Display calculated result
		print("\n (" + k + ") Least significant Bit is : " + bit);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitPosition = new BitPosition();
		// 35 = (100011)
		var n: Int = 35;
		var k: Int = 6;
		task.leastSignificant(n, k);
		k = 3;
		task.leastSignificant(n, k);
		k = 2;
		task.leastSignificant(n, k);
	}
}

Output

 Number : 35
 (6) Least significant Bit is : 1
 Number : 35
 (3) Least significant Bit is : 0
 Number : 35
 (2) Least significant Bit is : 1
/*
  Swift 4 Program for
  Find the kth least significant bit of a number
*/
class BitPosition
{
	// Find kth least significant bit
	func leastSignificant(_ n: Int, _ k: Int)
	{
		if (k <= 0)
		{
			return;
		}
		// Find kth bit
		var bit: Int = ((1 << (k - 1))) & n;
		if (bit  != 0)
		{
			bit = 1;
		}
		print("\n Number : ", n, terminator: "");
		// Display calculated result
		print("\n (", k ,") Least significant Bit is : ", bit, terminator: "");
	}
}
func main()
{
	let task: BitPosition = BitPosition();
	// 35 = (100011)
	let n: Int = 35;
	var k: Int = 6;
	task.leastSignificant(n, k);
	k = 3;
	task.leastSignificant(n, k);
	k = 2;
	task.leastSignificant(n, k);
}
main();

Output

 Number :  35
 ( 6 ) Least significant Bit is :  1
 Number :  35
 ( 3 ) Least significant Bit is :  0
 Number :  35
 ( 2 ) Least significant Bit is :  1
/*
  Kotlin Program for
  Find the kth least significant bit of a number
*/
class BitPosition
{
	// Find kth least significant bit
	fun leastSignificant(n: Int, k: Int): Unit
	{
		if (k <= 0)
		{
			return;
		}
		// Find kth bit
		var bit: Int = ((1 shl (k - 1))) and n;
		if (bit != 0)
		{
			bit = 1;
		}
		print("\n Number : " + n);
		// Display calculated result
		print("\n (" + k + ") Least significant Bit is : " + bit);
	}
}
fun main(args: Array < String > ): Unit
{
	var task: BitPosition = BitPosition();
	// 35 = (100011)
	var n: Int = 35;
	var k: Int = 6;
	task.leastSignificant(n, k);
	k = 3;
	task.leastSignificant(n, k);
	k = 2;
	task.leastSignificant(n, k);
}

Output

 Number : 35
 (6) Least significant Bit is : 1
 Number : 35
 (3) Least significant Bit is : 0
 Number : 35
 (2) Least significant Bit is : 1

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