Activate all the even position bits of a number

Here given code implementation process.

// C Program 
// Activate all the even position bits of a number
#include <stdio.h>

// Set all even position bit of a number
void activeEvenBits(int num)
{
	int result = num;
	// Active all bits 
	result = (result >> 1) | result;
	result = (result >> 2) | result;
	result = (result >> 4) | result;
	result = (result >> 8) | result;
	result = (result >> 16) | result;
	result = (result + 1) >> 1;
	// Select Even bits
	result = (result >> 2) | result;
	result = (result >> 4) | result;
	result = (result >> 8) | result;
	result = (result >> 16) | result;
	if ((result & 1) == 1)
	{
		result = result >> 1;
	}
	result = result | num;
	// Display given number
	printf(" Number : %d", num);
	// After activating all Even position bit
	printf("\n Output : %d\n\n", result);
}
int main()
{
	// Test cases
	// 10000 => 11010 (26)
	activeEvenBits(16);
	// 100011 => 101011 (43)
	activeEvenBits(35);
	// 10100  => 11110 (30)
	activeEvenBits(20);
	return 0;
}

Output

 Number : 16
 Output : 26

 Number : 35
 Output : 43

 Number : 20
 Output : 30
/*
  Java Program for
  Activate all the even position bits of a number
*/
public class ActivateBits
{
    // Set all even position bit of a number
    public void activeEvenBits(int num)
    {
        int result = num;
        // Active all bits 
        result = (result >> 1) | result;
        result = (result >> 2) | result;
        result = (result >> 4) | result;
        result = (result >> 8) | result;
        result = (result >> 16) | result;
        result = (result + 1) >> 1;
        // Select Even bits
        result = (result >> 2) | result;
        result = (result >> 4) | result;
        result = (result >> 8) | result;
        result = (result >> 16) | result;
        if ((result & 1) == 1)
        {
            result = result >> 1;
        }
        result = result | num;
        // Display given number
        System.out.print(" Number : " + num );
        // After activating all Even position bit
        System.out.print("\n Output : " + result + "\n\n");
    }
    public static void main(String[] args)
    {
        ActivateBits task = new ActivateBits();
        // Test cases
        // 10000 => 11010 (26)
        task.activeEvenBits(16);
        // 100011 => 101011 (43)
        task.activeEvenBits(35);
        // 10100  => 11110 (30)
        task.activeEvenBits(20);
    }
}

Output

 Number : 16
 Output : 26

 Number : 35
 Output : 43

 Number : 20
 Output : 30
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program for
  Activate all the even position bits of a number
*/

class ActivateBits
{
	public:
		// Set all even position bit of a number
		void activeEvenBits(int num)
		{
			int result = num;
			// Active all bits
			result = (result >> 1) | result;
			result = (result >> 2) | result;
			result = (result >> 4) | result;
			result = (result >> 8) | result;
			result = (result >> 16) | result;
			result = (result + 1) >> 1;
			// Select Even bits
			result = (result >> 2) | result;
			result = (result >> 4) | result;
			result = (result >> 8) | result;
			result = (result >> 16) | result;
			if ((result &1) == 1)
			{
				result = result >> 1;
			}
			result = result | num;
			// Display given number
			cout << " Number : " << num;
			// After activating all Even position bit
			cout << "\n Output : " << result << "\n\n";
		}
};
int main()
{
	ActivateBits task = ActivateBits();
	// Test cases
	// 10000 => 11010 (26)
	task.activeEvenBits(16);
	// 100011 => 101011 (43)
	task.activeEvenBits(35);
	// 10100  => 11110 (30)
	task.activeEvenBits(20);
	return 0;
}

Output

 Number : 16
 Output : 26

 Number : 35
 Output : 43

 Number : 20
 Output : 30
// Include namespace system
using System;
/*
  C# Program for
  Activate all the even position bits of a number
*/
public class ActivateBits
{
	// Set all even position bit of a number
	public void activeEvenBits(int num)
	{
		int result = num;
		// Active all bits
		result = (result >> 1) | result;
		result = (result >> 2) | result;
		result = (result >> 4) | result;
		result = (result >> 8) | result;
		result = (result >> 16) | result;
		result = (result + 1) >> 1;
		// Select Even bits
		result = (result >> 2) | result;
		result = (result >> 4) | result;
		result = (result >> 8) | result;
		result = (result >> 16) | result;
		if ((result & 1) == 1)
		{
			result = result >> 1;
		}
		result = result | num;
		// Display given number
		Console.Write(" Number : " + num);
		// After activating all Even position bit
		Console.Write("\n Output : " + result + "\n\n");
	}
	public static void Main(String[] args)
	{
		ActivateBits task = new ActivateBits();
		// Test cases
		// 10000 => 11010 (26)
		task.activeEvenBits(16);
		// 100011 => 101011 (43)
		task.activeEvenBits(35);
		// 10100  => 11110 (30)
		task.activeEvenBits(20);
	}
}

Output

 Number : 16
 Output : 26

 Number : 35
 Output : 43

 Number : 20
 Output : 30
<?php
/*
  Php Program for
  Activate all the even position bits of a number
*/
class ActivateBits
{
	// Set all even position bit of a number
	public	function activeEvenBits($num)
	{
		$result = $num;
		// Active all bits
		$result = ($result >> 1) | $result;
		$result = ($result >> 2) | $result;
		$result = ($result >> 4) | $result;
		$result = ($result >> 8) | $result;
		$result = ($result >> 16) | $result;
		$result = ($result + 1) >> 1;
		// Select Even bits
		$result = ($result >> 2) | $result;
		$result = ($result >> 4) | $result;
		$result = ($result >> 8) | $result;
		$result = ($result >> 16) | $result;
		if (($result & 1) == 1)
		{
			$result = $result >> 1;
		}
		$result = $result | $num;
		// Display given number
		echo " Number : ". $num;
		// After activating all Even position bit
		echo "\n Output : ". $result ."\n\n";
	}
}

function main()
{
	$task = new ActivateBits();
	// Test cases
	// 10000 => 11010 (26)
	$task->activeEvenBits(16);
	// 100011 => 101011 (43)
	$task->activeEvenBits(35);
	// 10100  => 11110 (30)
	$task->activeEvenBits(20);
}
main();

Output

 Number : 16
 Output : 26

 Number : 35
 Output : 43

 Number : 20
 Output : 30
/*
  Node Js Program for
  Activate all the even position bits of a number
*/
class ActivateBits
{
	// Set all even position bit of a number
	activeEvenBits(num)
	{
		var result = num;
		// Active all bits
		result = (result >> 1) | result;
		result = (result >> 2) | result;
		result = (result >> 4) | result;
		result = (result >> 8) | result;
		result = (result >> 16) | result;
		result = (result + 1) >> 1;
		// Select Even bits
		result = (result >> 2) | result;
		result = (result >> 4) | result;
		result = (result >> 8) | result;
		result = (result >> 16) | result;
		if ((result & 1) == 1)
		{
			result = result >> 1;
		}
		result = result | num;
		// Display given number
		process.stdout.write(" Number : " + num);
		// After activating all Even position bit
		process.stdout.write("\n Output : " + result + "\n\n");
	}
}

function main()
{
	var task = new ActivateBits();
	// Test cases
	// 10000 => 11010 (26)
	task.activeEvenBits(16);
	// 100011 => 101011 (43)
	task.activeEvenBits(35);
	// 10100  => 11110 (30)
	task.activeEvenBits(20);
}
main();

Output

 Number : 16
 Output : 26

 Number : 35
 Output : 43

 Number : 20
 Output : 30
#   Python 3 Program for
#   Activate all the even position bits of a number

class ActivateBits :
	#  Set all even position bit of a number
	def activeEvenBits(self, num) :
		result = num
		#  Active all bits 
		result = (result >> 1) | result
		result = (result >> 2) | result
		result = (result >> 4) | result
		result = (result >> 8) | result
		result = (result >> 16) | result
		result = (result + 1) >> 1
		#  Select Even bits
		result = (result >> 2) | result
		result = (result >> 4) | result
		result = (result >> 8) | result
		result = (result >> 16) | result
		if ((result & 1) == 1) :
			result = result >> 1
		
		result = result | num
		#  Display given number
		print(" Number : ", num, end = "")
		#  After activating all Even position bit
		print("\n Output : ", result ,"\n")
	

def main() :
	task = ActivateBits()
	#  Test cases
	#  10000 => 11010 (26)
	task.activeEvenBits(16)
	#  100011 => 101011 (43)
	task.activeEvenBits(35)
	#  10100  => 11110 (30)
	task.activeEvenBits(20)

if __name__ == "__main__": main()

Output

 Number :  16
 Output :  26

 Number :  35
 Output :  43

 Number :  20
 Output :  30
#   Ruby Program for
#   Activate all the even position bits of a number

class ActivateBits 
	#  Set all even position bit of a number
	def activeEvenBits(num) 
		result = num
		#  Active all bits 
		result = (result >> 1) | result
		result = (result >> 2) | result
		result = (result >> 4) | result
		result = (result >> 8) | result
		result = (result >> 16) | result
		result = (result + 1) >> 1
		#  Select Even bits
		result = (result >> 2) | result
		result = (result >> 4) | result
		result = (result >> 8) | result
		result = (result >> 16) | result
		if ((result & 1) == 1) 
			result = result >> 1
		end

		result = result | num
		#  Display given number
		print(" Number : ", num)
		#  After activating all Even position bit
		print("\n Output : ", result ,"\n\n")
	end

end

def main() 
	task = ActivateBits.new()
	#  Test cases
	#  10000 => 11010 (26)
	task.activeEvenBits(16)
	#  100011 => 101011 (43)
	task.activeEvenBits(35)
	#  10100  => 11110 (30)
	task.activeEvenBits(20)
end

main()

Output

 Number : 16
 Output : 26

 Number : 35
 Output : 43

 Number : 20
 Output : 30

/*
  Scala Program for
  Activate all the even position bits of a number
*/
class ActivateBits
{
	// Set all even position bit of a number
	def activeEvenBits(num: Int): Unit = {
		var result: Int = num;
		// Active all bits
		result = (result >> 1) | result;
		result = (result >> 2) | result;
		result = (result >> 4) | result;
		result = (result >> 8) | result;
		result = (result >> 16) | result;
		result = (result + 1) >> 1;
		// Select Even bits
		result = (result >> 2) | result;
		result = (result >> 4) | result;
		result = (result >> 8) | result;
		result = (result >> 16) | result;
		if ((result & 1) == 1)
		{
			result = result >> 1;
		}
		result = result | num;
		// Display given number
		print(" Number : " + num);
		// After activating all Even position bit
		print("\n Output : " + result + "\n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: ActivateBits = new ActivateBits();
		// Test cases
		// 10000 => 11010 (26)
		task.activeEvenBits(16);
		// 100011 => 101011 (43)
		task.activeEvenBits(35);
		// 10100  => 11110 (30)
		task.activeEvenBits(20);
	}
}

Output

 Number : 16
 Output : 26

 Number : 35
 Output : 43

 Number : 20
 Output : 30
/*
  Swift 4 Program for
  Activate all the even position bits of a number
*/
class ActivateBits
{
	// Set all even position bit of a number
	func activeEvenBits(_ num: Int)
	{
		var result: Int = num;
		// Active all bits
		result = (result >> 1) | result;
		result = (result >> 2) | result;
		result = (result >> 4) | result;
		result = (result >> 8) | result;
		result = (result >> 16) | result;
		result = (result + 1) >> 1;
		// Select Even bits
		result = (result >> 2) | result;
		result = (result >> 4) | result;
		result = (result >> 8) | result;
		result = (result >> 16) | result;
		if ((result & 1) == 1)
		{
			result = result >> 1;
		}
		result = result | num;
		// Display given number
		print(" Number : ", num, terminator: "");
		// After activating all Even position bit
		print("\n Output : ", result ,"\n");
	}
}
func main()
{
	let task: ActivateBits = ActivateBits();
	// Test cases
	// 10000 => 11010 (26)
	task.activeEvenBits(16);
	// 100011 => 101011 (43)
	task.activeEvenBits(35);
	// 10100  => 11110 (30)
	task.activeEvenBits(20);
}
main();

Output

 Number :  16
 Output :  26

 Number :  35
 Output :  43

 Number :  20
 Output :  30
/*
  Kotlin Program for
  Activate all the even position bits of a number
*/
class ActivateBits
{
	// Set all even position bit of a number
	fun activeEvenBits(num: Int): Unit
	{
		var result: Int = num;
		// Active all bits
		result = (result shr 1) or result;
		result = (result shr 2) or result;
		result = (result shr 4) or result;
		result = (result shr 8) or result;
		result = (result shr 16) or result;
		result = (result + 1) shr 1;
		// Select Even bits
		result = (result shr 2) or result;
		result = (result shr 4) or result;
		result = (result shr 8) or result;
		result = (result shr 16) or result;
		if ((result and 1) == 1)
		{
			result = result shr 1;
		}
		result = result or num;
		// Display given number
		print(" Number : " + num);
		// After activating all Even position bit
		print("\n Output : " + result + "\n\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: ActivateBits = ActivateBits();
	// Test cases
	// 10000 => 11010 (26)
	task.activeEvenBits(16);
	// 100011 => 101011 (43)
	task.activeEvenBits(35);
	// 10100  => 11110 (30)
	task.activeEvenBits(20);
}

Output

 Number : 16
 Output : 26

 Number : 35
 Output : 43

 Number : 20
 Output : 30


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