Posted on by Kalkicode
Code Bit Logic

Activate all the even position bits of a number

In this article, we will discuss a problem where we need to activate all the even position bits of a given number. A bit is considered to be at an even position if its index is divisible by 2. For example, in the binary representation of a number "1010010," the even position bits are at index 0, 2, 4, and so on.

Problem Statement

Given a non-negative integer, our task is to activate all the even position bits and return the resulting number.

Explanation with Suitable Example

Let's take the number 20 (binary: 10100) as an example. The even position bits are at indices 0, 2, and 4. By activating these bits, we get the binary number 11110, which is equal to the decimal number 30.

Pseudocode

activeEvenBits(num):
    result = num
    // Activate 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
    return result
  

Algorithm Explanation

  1. Initialize a variable "result" with the input number.
  2. Shift "result" right by 1 and perform bitwise OR with the original "result" to activate every second bit.
  3. Repeat step 2 for shifting by 2, 4, 8, and 16 bits.
  4. Add 1 to "result" and shift it right by 1 to set all bits to 1 from the least significant bit up to the first even bit.
  5. Repeat step 2 for shifting by 2, 4, 8, and 16 bits again to select only the even position bits.
  6. If the least significant bit of "result" is 1, shift "result" right by 1 to eliminate the extra bit.
  7. Perform a bitwise OR operation between "result" and the input number to combine the activated even bits with the original bits.
  8. Return the final "result."

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

Resultant Output Explanation

  • activeEvenBits(16) returns 26 because binary(16) is 10000, and activating the even bits gives binary(26) 11010.
  • activeEvenBits(35) returns 43 because binary(35) is 100011, and activating the even bits gives binary(43) 101011.
  • activeEvenBits(20) returns 30 because binary(20) is 10100, and activating the even bits gives binary(30) 11110.

Time Complexity

The time complexity of the given code is O(1) since the number of iterations remains constant, independent of the input value.

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