Shift active bits to the right side

Here given code implementation process.

// C Program
// Shift active bits to the right side
#include <stdio.h>

// Returns the number of active set bits in positive integer
int countSetBit(int num)
{
	int count = 0;
	while (num > 0)
	{
		count++;
		num = num & (num - 1);
	}
	// Returning the value of calculate result
	return count;
}
// Handles the request of shift all active bits in right side
void shiftActiveBits(int num)
{
	if (num < 0)
	{
		return;
	}
	printf("\n Given num : %d", num);
	// shift active bits
	int result = (1 << countSetBit(num)) - 1;
	printf("\n After Shift : %d\n", result);
}
int main(int argc, char
	const *argv[])
{
	// num = 21
	// 21 = (10101) 3 bit is active
	// 7  = (111)
	shiftActiveBits(21);
	// num = 12  
	// 12 => 1100
	// 2 bit is active
	// (3)   11 
	shiftActiveBits(12);
	return 0;
}

Output

 Given num : 21
 After Shift : 7

 Given num : 12
 After Shift : 3
/*
  Java program
  Shift active bits to the right side
*/
public class BitManipulation
{
	// Returns the number of active set bits in positive integer
	public int countSetBit(int num)
	{
		int count = 0;
		while (num > 0)
		{
			count++;
			num = num & (num - 1);
		}
		// Returning the value of calculate result
		return count;
	}
	// Handles the request of shift all active bits in right side
	public void shiftActiveBits(int num)
	{
		if (num < 0)
		{
			return;
		}
		System.out.print("\n Given num : " + num);
		// shift active bits
		int result = (1 << countSetBit(num)) - 1;
		System.out.print("\n After Shift : " + result + "\n");
	}
	public static void main(String[] args)
	{
		BitManipulation task = new BitManipulation();
		// num = 21
		// 21 = (10101) 3 bit is active
		// 7  = (111)
		task.shiftActiveBits(21);
		// num = 12  
		// 12 => 1100
		// 2 bit is active
		// (3)   11 
		task.shiftActiveBits(12);
	}
}

Output

 Given num : 21
 After Shift : 7

 Given num : 12
 After Shift : 3
// Include header file
#include <iostream>
using namespace std;

/*
  C++ program
  Shift active bits to the right side
*/

class BitManipulation
{
	public:
		// Returns the number of active set bits in positive integer
		int countSetBit(int num)
		{
			
			int count = 0;
			while (num > 0)
			{
				count++;
				num = num &(num - 1);
			}
                       // Returning the value of calculate result
			return count;
		}
	// Handles the request of shift all active bits in right side
	void shiftActiveBits(int num)
	{
		if (num < 0)
		{
			return;
		}
		cout << "\n Given num : " << num;
		// shift active bits
		int result = (1 << this->countSetBit(num)) - 1;
		cout << "\n After Shift : " << result << "\n";
	}
};
int main()
{
	BitManipulation task = BitManipulation();
	// num = 21
	// 21 = (10101) 3 bit is active
	// 7  = (00111)
	task.shiftActiveBits(21);
	// num = 12
	// 12 => 1100
	// 2 bit is active
	// (3)   0011
	task.shiftActiveBits(12);
	return 0;
}

Output

 Given num : 21
 After Shift : 7

 Given num : 12
 After Shift : 3
// Include namespace system
using System;
/*
  C# program
  Shift active bits to the right side
*/
public class BitManipulation
{
	// Returns the number of active set bits in positive integer
	public int countSetBit(int num)
	{

		int count = 0;
		while (num > 0)
		{
			count++;
			num = num & (num - 1);
		}
		// Returning the value of calculate result
		return count;
	}
	// Handles the request of shift all active bits in right side
	public void shiftActiveBits(int num)
	{
		if (num < 0)
		{
			return;
		}
		Console.Write("\n Given num : " + num);
		// shift active bits
		int result = (1 << countSetBit(num)) - 1;
		Console.Write("\n After Shift : " + result + "\n");
	}
	public static void Main(String[] args)
	{
		BitManipulation task = new BitManipulation();
		// num = 21
		// 21 = (10101) 3 bit is active
		// 7  = (00111)
		task.shiftActiveBits(21);
		// num = 12
		// 12 => 1100
		// 2 bit is active
		// (3)   0011
		task.shiftActiveBits(12);
	}
}

Output

 Given num : 21
 After Shift : 7

 Given num : 12
 After Shift : 3
<?php
/*
  Php program
  Shift active bits to the right side
*/
class BitManipulation
{
	// Returns the number of active set bits in positive integer
	public	function countSetBit($num)
	{

		$count = 0;
		while ($num > 0)
		{
			$count++;
			$num = $num & ($num - 1);
		}
		// Returning the value of calculate result
		return $count;
	}
	// Handles the request of shift all active bits in right side
	public	function shiftActiveBits($num)
	{
		if ($num < 0)
		{
			return;
		}
		echo "\n Given num : ". $num;
		// shift active bits
		$result = (1 << $this->countSetBit($num)) - 1;
		echo "\n After Shift : ". $result ."\n";
	}
}

function main()
{
	$task = new BitManipulation();
	// num = 21
	// 21 = (10101) 3 bit is active
	// 7  = (00111)
	$task->shiftActiveBits(21);
	// num = 12
	// 12 => 1100
	// 2 bit is active
	// (3)   0011
	$task->shiftActiveBits(12);
}
main();

Output

 Given num : 21
 After Shift : 7

 Given num : 12
 After Shift : 3
/*
  Node Js program
  Shift active bits to the right side
*/
class BitManipulation
{
	// Returns the number of active set bits in positive integer
	countSetBit(num)
	{

		var count = 0;
		while (num > 0)
		{
			count++;
			num = num & (num - 1);
		}
		// Returning the value of calculate result
		return count;
	}
	// Handles the request of shift all active bits in right side
	shiftActiveBits(num)
	{
		if (num < 0)
		{
			return;
		}
		process.stdout.write("\n Given num : " + num);
		// shift active bits
		var result = (1 << this.countSetBit(num)) - 1;
		process.stdout.write("\n After Shift : " + result + "\n");
	}
}

function main()
{
	var task = new BitManipulation();
	// num = 21
	// 21 = (10101) 3 bit is active
	// 7  = (00111)
	task.shiftActiveBits(21);
	// num = 12
	// 12 => 1100
	// 2 bit is active
	// (3)   0011
	task.shiftActiveBits(12);
}
main();

Output

 Given num : 21
 After Shift : 7

 Given num : 12
 After Shift : 3
#   Python 3 program
#   Shift active bits to the right side

class BitManipulation :
	#  Returns the number of active set bits in positive integer
	def countSetBit(self, num) :
		count = 0
		while (num > 0) :
			count += 1
			num = num & (num - 1)
		
		#  Returning the value of calculate result
		return count
	
	#  Handles the request of shift all active bits in right side
	def shiftActiveBits(self, num) :
		if (num < 0) :
			return
		
		print("\n Given num : ", num, end = "")
		#  shift active bits
		result = (1 << self.countSetBit(num)) - 1
		print("\n After Shift : ", result )
	

def main() :
	task = BitManipulation()
	#  num = 21
	#  21 = (10101) 3 bit is active
	#  7  = (00111)
	task.shiftActiveBits(21)
	#  num = 12  
	#  12 => 1100
	#  2 bit is active
	#  (3)   0011 
	task.shiftActiveBits(12)

if __name__ == "__main__": main()

Output

 Given num :  21
 After Shift :  7

 Given num :  12
 After Shift :  3
#   Ruby program
#   Shift active bits to the right side

class BitManipulation 
	#  Returns the number of active set bits in positive integer
	def countSetBit(num) 
		count = 0
		while (num > 0) 
			count += 1
			num = num & (num - 1)
		end

		#  Returning the value of calculate result
		return count
	end

	#  Handles the request of shift all active bits in right side
	def shiftActiveBits(num) 
		if (num < 0) 
			return
		end

		print("\n Given num : ", num)
		#  shift active bits
		result = (1 << self.countSetBit(num)) - 1
		print("\n After Shift : ", result ,"\n")
	end

end

def main() 
	task = BitManipulation.new()
	#  num = 21
	#  21 = (10101) 3 bit is active
	#  7  = (00111)
	task.shiftActiveBits(21)
	#  num = 12  
	#  12 => 1100
	#  2 bit is active
	#  (3)   0011 
	task.shiftActiveBits(12)
end

main()

Output

 Given num : 21
 After Shift : 7

 Given num : 12
 After Shift : 3
/*
  Scala program
  Shift active bits to the right side
*/
class BitManipulation
{
	// Returns the number of active set bits in positive integer
	def countSetBit(n: Int): Int = {

		var count: Int = 0;
      	var num = n;
		while (num > 0)
		{
			count += 1;
			num = num & (num - 1);
		}
		// Returning the value of calculate result
		return count;
	}
	// Handles the request of shift all active bits in right side
	def shiftActiveBits(num: Int): Unit = {
		if (num < 0)
		{
			return;
		}
		print("\n Given num : " + num);
		// shift active bits
		var result: Int = (1 << this.countSetBit(num)) - 1;
		print("\n After Shift : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitManipulation = new BitManipulation();
		// num = 21
		// 21 = (10101) 3 bit is active
		// 7  = (00111)
		task.shiftActiveBits(21);
		// num = 12
		// 12 => 1100
		// 2 bit is active
		// (3)   0011
		task.shiftActiveBits(12);
	}
}

Output

 Given num : 21
 After Shift : 7

 Given num : 12
 After Shift : 3
/*
  Swift 4 program
  Shift active bits to the right side
*/
class BitManipulation
{
	// Returns the number of active set bits in positive integer
	func countSetBit(_ n: Int)->Int
	{

		var count: Int = 0;
      	var num = n;
		while (num > 0)
		{
			count += 1;
			num = num & (num - 1);
		}
		// Returning the value of calculate result
		return count;
	}
	// Handles the request of shift all active bits in right side
	func shiftActiveBits(_ num: Int)
	{
		if (num < 0)
		{
			return;
		}
		print("\n Given num : ", num, terminator: "");
		// shift active bits
		let result: Int = (1 << self.countSetBit(num)) - 1;
		print("\n After Shift : ", result );
	}
}
func main()
{
	let task: BitManipulation = BitManipulation();
	// num = 21
	// 21 = (10101) 3 bit is active
	// 7  = (00111)
	task.shiftActiveBits(21);
	// num = 12
	// 12 => 1100
	// 2 bit is active
	// (3)   0011
	task.shiftActiveBits(12);
}
main();

Output

 Given num :  21
 After Shift :  7

 Given num :  12
 After Shift :  3
/*
  Kotlin program
  Shift active bits to the right side
*/
class BitManipulation
{
	// Returns the number of active set bits in positive integer
	fun countSetBit(n: Int): Int
	{
	
		var count: Int = 0;
     	var num: Int = n;
		while (num > 0)
		{
			count += 1;
			num = num and(num - 1);
		}
      	// Returning the value of calculate result
		return count;
	}
	// Handles the request of shift all active bits in right side
	fun shiftActiveBits(num: Int): Unit
	{
		if (num < 0)
		{
			return;
		}
		print("\n Given num : " + num);
		// shift active bits
		var result: Int = (1 shl this.countSetBit(num)) - 1;
		print("\n After Shift : " + result + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: BitManipulation = BitManipulation();
	// num = 21
	// 21 = (10101) 3 bit is active
	// 7  = (00111)
	task.shiftActiveBits(21);
	// num = 12
	// 12 => 1100
	// 2 bit is active
	// (3)   0011
	task.shiftActiveBits(12);
}

Output

 Given num : 21
 After Shift : 7

 Given num : 12
 After Shift : 3


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