Skip to main content

Append a set bit in given position of a number

The given problem is about appending a set bit at a given position in a number. The goal is to take an integer num and a positive integer location, and then set the bit at the location in the binary representation of the number to 1. The binary representation is considered to have positions numbered from right to left, starting from 1.

Problem Statement:

Given an integer num and a positive integer location, the task is to set the bit at the location of num to 1 and return the modified number.

Example:

Let's take an example to illustrate the problem:

  • num = 17 (binary representation: 10001)
  • location = 3

We need to set the bit at the 3rd location (from right) to 1:

  • Original binary representation: 10001
  • After setting the 3rd bit (from right) to 1: 100101
  • The result is 37.

Pseudocode:

function appendActiveBit(num, location):
    if num < 0 or location < 1:
        return num
    
    left = num & ((1 << (location - 1)) - 1)
    result = num - left
    result = (result << 1) ^ (1 << (location - 1))
    result = result + left
    return result

Algorithm Explanation:

  1. Check if the given number num is non-negative (negative numbers don't have a valid binary representation with leading zeros) and if the given location is positive.
  2. Compute the left variable by performing a bitwise AND operation between the number and a mask to get the bits to the right of the location.
  3. Subtract left from the number to set all the bits to the right of location to 0.
  4. Left-shift the modified number by 1 to make room for the new set bit.
  5. XOR the result with a mask created by left-shifting 1 by location - 1, effectively setting the location-th bit to 1.
  6. Add back the left to the result to retain its original value.

Code Execution Explanation:

  1. The main function calls the appendActiveBit function with three different sets of input values (num and location).
  2. The first input set is num = 17 and location = 3. The function sets the 3rd bit of num to 1, resulting in 37.
  3. The second input set is num = 6 and location = 5. The function sets the 5th bit of num to 1, resulting in 22.
  4. The third input set is num = 6 and location = 1. The function sets the 1st bit of num to 1, resulting in 13.

Code Solution

Here given code implementation process.

// C Program
// Append a set bit in given position of a number
#include <stdio.h>

// Append a active bits in given valid positions
void appendActiveBit(int num, int location)
{
	if (num < 0 || location < 1)
	{
		return;
	}
	// Get the left side part of a append bits in given number
	int left = (num) & ((1 << (location - 1)) - 1);
	// Inactive the all bits of left side part 
	int result = num - left;
	// Add Active bit at given location
	result = (result << 1) ^ (1 << location - 1);
	// Add left part
	result = result + left;
  
	// Display given values
	printf("\n Given Number    : %d", num);
	printf("\n Append Location : %d", location);
  
	// Display calculated result 
	printf("\n Output          : %d \n", result);
}
int main(int argc, char
	const *argv[])
{
	int num = 17;
	int location = 3;
	// (17) = (10001)
	// Add active bit at the 3rd location
	// 10001 => 100 + 1 + 01
	//       => 100101 (37)
	appendActiveBit(num, location);
	num = 6;
	location = 5;
	// (6) = (00110)
	// Add active bit at the 5th location
	// 00110 => 0+1+0110
	//       => 010110 (22)
	appendActiveBit(num, location);
	num = 6;
	location = 1;
	// (6) = (110)
	// Add active bit at the 5th location
	// 110 => 110+1
	//     => 1101 (13)
	appendActiveBit(num, location);
	return 0;
}

Output

 Given Number    : 17
 Append Location : 3
 Output          : 37

 Given Number    : 6
 Append Location : 5
 Output          : 22

 Given Number    : 6
 Append Location : 1
 Output          : 13
/*
  Java program
  Append a set bit in given position of a number
*/
public class BitManipulation
{
	// Append a active bits in given valid positions
	public void appendActiveBit(int num, int location)
	{
		if (num < 0 || location < 1)
		{
			return;
		}
		// Get the left side part of a append bits in given number
		int left = (num) & ((1 << (location - 1)) - 1);
		// Inactive the all bits of left side part 
		int result = num - left;
		// Add Active bit at given location
		result = (result << 1) ^ (1 << location - 1);
		// Add left part
		result = result + left;
		// Display given values
		System.out.print("\n Given Number : " + num);
		System.out.print("\n Append Location : " + location);
		// Display calculated result 
		System.out.print("\n Output : " + result + " \n");
	}
	public static void main(String[] args)
	{
		BitManipulation task = new BitManipulation();
		int num = 17;
		int location = 3;
		// (17) = (10001)
		// Add active bit at the 3rd location
		// 10001 => 100 + 1 + 01
		//       => 100101 (37)
		task.appendActiveBit(num, location);
		// new case
		num = 6;
		location = 5;
		// (6) = (00110)
		// Add active bit at the 5th location
		// 00110 => 0+1+0110
		//       => 010110 (22)
		task.appendActiveBit(num, location);
		// new case
		num = 6;
		location = 1;
		// (6) = (110)
		// Add active bit at the 5th location
		// 110 => 110+1
		//     => 1101 (13)
		task.appendActiveBit(num, location);
	}
}

Output

 Given Number : 17
 Append Location : 3
 Output : 37

 Given Number : 6
 Append Location : 5
 Output : 22

 Given Number : 6
 Append Location : 1
 Output : 13
// Include header file
#include <iostream>
using namespace std;

/*
  C++ program
  Append a set bit in given position of a number
*/

class BitManipulation
{
	public:
		// Append a active bits in given valid positions
		void appendActiveBit(int num, int location)
		{
			if (num < 0 || location < 1)
			{
				return;
			}
			// Get the left side part of a append bits in given number
			int left = (num) &((1 << (location - 1)) - 1);
			// Inactive the all bits of left side part
			int result = num - left;
			// Add Active bit at given location
			result = (result << 1) ^ (1 << location - 1);
			// Add left part
			result = result + left;
			// Display given values
			cout << "\n Given Number : " << num;
			cout << "\n Append Location : " << location;
			// Display calculated result
			cout << "\n Output : " << result << " \n";
		}
};
int main()
{
	BitManipulation task = BitManipulation();
	int num = 17;
	int location = 3;
	// (17) = (10001)
	// Add active bit at the 3rd location
	// 10001 => 100 + 1 + 01
	//       => 100101 (37)
	task.appendActiveBit(num, location);
	// new case
	num = 6;
	location = 5;
	// (6) = (00110)
	// Add active bit at the 5th location
	// 00110 => 0+1+0110
	//       => 010110 (22)
	task.appendActiveBit(num, location);
	// new case
	num = 6;
	location = 1;
	// (6) = (110)
	// Add active bit at the 5th location
	// 110 => 110+1
	//     => 1101 (13)
	task.appendActiveBit(num, location);
	return 0;
}

Output

 Given Number : 17
 Append Location : 3
 Output : 37

 Given Number : 6
 Append Location : 5
 Output : 22

 Given Number : 6
 Append Location : 1
 Output : 13
// Include namespace system
using System;
/*
  C# program
  Append a set bit in given position of a number
*/
public class BitManipulation
{
	// Append a active bits in given valid positions
	public void appendActiveBit(int num, int location)
	{
		if (num < 0 || location < 1)
		{
			return;
		}
		// Get the left side part of a append bits in given number
		int left = (num) & ((1 << (location - 1)) - 1);
		// Inactive the all bits of left side part
		int result = num - left;
		// Add Active bit at given location
		result = (result << 1) ^ (1 << location - 1);
		// Add left part
		result = result + left;
		// Display given values
		Console.Write("\n Given Number : " + num);
		Console.Write("\n Append Location : " + location);
		// Display calculated result
		Console.Write("\n Output : " + result + " \n");
	}
	public static void Main(String[] args)
	{
		BitManipulation task = new BitManipulation();
		int num = 17;
		int location = 3;
		// (17) = (10001)
		// Add active bit at the 3rd location
		// 10001 => 100 + 1 + 01
		//       => 100101 (37)
		task.appendActiveBit(num, location);
		// new case
		num = 6;
		location = 5;
		// (6) = (00110)
		// Add active bit at the 5th location
		// 00110 => 0+1+0110
		//       => 010110 (22)
		task.appendActiveBit(num, location);
		// new case
		num = 6;
		location = 1;
		// (6) = (110)
		// Add active bit at the 5th location
		// 110 => 110+1
		//     => 1101 (13)
		task.appendActiveBit(num, location);
	}
}

Output

 Given Number : 17
 Append Location : 3
 Output : 37

 Given Number : 6
 Append Location : 5
 Output : 22

 Given Number : 6
 Append Location : 1
 Output : 13
<?php
/*
  Php program
  Append a set bit in given position of a number
*/
class BitManipulation
{
	// Append a active bits in given valid positions
	public	function appendActiveBit($num, $location)
	{
		if ($num < 0 || $location < 1)
		{
			return;
		}
		// Get the left side part of a append bits in given number
		$left = ($num) & ((1 << ($location - 1)) - 1);
		// Inactive the all bits of left side part
		$result = $num - $left;
		// Add Active bit at given location
		$result = ($result << 1) ^ (1 << $location - 1);
		// Add left part
		$result = $result + $left;
		// Display given values
		echo "\n Given Number : ". $num;
		echo "\n Append Location : ". $location;
		// Display calculated result
		echo "\n Output : ". $result ." \n";
	}
}

function main()
{
	$task = new BitManipulation();
	$num = 17;
	$location = 3;
	// (17) = (10001)
	// Add active bit at the 3rd location
	// 10001 => 100 + 1 + 01
	//       => 100101 (37)
	$task->appendActiveBit($num, $location);
	// new case
	$num = 6;
	$location = 5;
	// (6) = (00110)
	// Add active bit at the 5th location
	// 00110 => 0+1+0110
	//       => 010110 (22)
	$task->appendActiveBit($num, $location);
	// new case
	$num = 6;
	$location = 1;
	// (6) = (110)
	// Add active bit at the 5th location
	// 110 => 110+1
	//     => 1101 (13)
	$task->appendActiveBit($num, $location);
}
main();

Output

 Given Number : 17
 Append Location : 3
 Output : 37

 Given Number : 6
 Append Location : 5
 Output : 22

 Given Number : 6
 Append Location : 1
 Output : 13
/*
  Node Js program
  Append a set bit in given position of a number
*/
class BitManipulation
{
	// Append a active bits in given valid positions
	appendActiveBit(num, location)
	{
		if (num < 0 || location < 1)
		{
			return;
		}
		// Get the left side part of a append bits in given number
		var left = (num) & ((1 << (location - 1)) - 1);
		// Inactive the all bits of left side part
		var result = num - left;
		// Add Active bit at given location
		result = (result << 1) ^ (1 << location - 1);
		// Add left part
		result = result + left;
		// Display given values
		process.stdout.write("\n Given Number : " + num);
		process.stdout.write("\n Append Location : " + location);
		// Display calculated result
		process.stdout.write("\n Output : " + result + " \n");
	}
}

function main()
{
	var task = new BitManipulation();
	var num = 17;
	var location = 3;
	// (17) = (10001)
	// Add active bit at the 3rd location
	// 10001 => 100 + 1 + 01
	//       => 100101 (37)
	task.appendActiveBit(num, location);
	// new case
	num = 6;
	location = 5;
	// (6) = (00110)
	// Add active bit at the 5th location
	// 00110 => 0+1+0110
	//       => 010110 (22)
	task.appendActiveBit(num, location);
	// new case
	num = 6;
	location = 1;
	// (6) = (110)
	// Add active bit at the 5th location
	// 110 => 110+1
	//     => 1101 (13)
	task.appendActiveBit(num, location);
}
main();

Output

 Given Number : 17
 Append Location : 3
 Output : 37

 Given Number : 6
 Append Location : 5
 Output : 22

 Given Number : 6
 Append Location : 1
 Output : 13
#   Python 3 program
#   Append a set bit in given position of a number

class BitManipulation :
	#  Append a active bits in given valid positions
	def appendActiveBit(self, num, location) :
		if (num < 0 or location < 1) :
			return
		
		#  Get the left side part of a append bits in given number
		left = (num) & ((1 << (location - 1)) - 1)
		#  Inactive the all bits of left side part 
		result = num - left
		#  Add Active bit at given location
		result = (result << 1) ^ (1 << location - 1)
		#  Add left part
		result = result + left
		#  Display given values
		print("\n Given Number : ", num, end = "")
		print("\n Append Location : ", location, end = "")
		#  Display calculated result 
		print("\n Output : ", result ," ")
	

def main() :
	task = BitManipulation()
	num = 17
	location = 3
	#  (17) = (10001)
	#  Add active bit at the 3rd location
	#  10001 => 100 + 1 + 01
	#        => 100101 (37)
	task.appendActiveBit(num, location)
	#  new case
	num = 6
	location = 5
	#  (6) = (00110)
	#  Add active bit at the 5th location
	#  00110 => 0+1+0110
	#        => 010110 (22)
	task.appendActiveBit(num, location)
	#  new case
	num = 6
	location = 1
	#  (6) = (110)
	#  Add active bit at the 5th location
	#  110 => 110+1
	#      => 1101 (13)
	task.appendActiveBit(num, location)

if __name__ == "__main__": main()

Output

 Given Number :  17
 Append Location :  3
 Output :  37

 Given Number :  6
 Append Location :  5
 Output :  22

 Given Number :  6
 Append Location :  1
 Output :  13
#   Ruby program
#   Append a set bit in given position of a number

class BitManipulation 
	#  Append a active bits in given valid positions
	def appendActiveBit(num, location) 
		if (num < 0 || location < 1) 
			return
		end

		#  Get the left side part of a append bits in given number
		left = (num) & ((1 << (location - 1)) - 1)
		#  Inactive the all bits of left side part 
		result = num - left
		#  Add Active bit at given location
		result = (result << 1) ^ (1 << location - 1)
		#  Add left part
		result = result + left
		#  Display given values
		print("\n Given Number : ", num)
		print("\n Append Location : ", location)
		#  Display calculated result 
		print("\n Output : ", result ," \n")
	end

end

def main() 
	task = BitManipulation.new()
	num = 17
	location = 3
	#  (17) = (10001)
	#  Add active bit at the 3rd location
	#  10001 => 100 + 1 + 01
	#        => 100101 (37)
	task.appendActiveBit(num, location)
	#  new case
	num = 6
	location = 5
	#  (6) = (00110)
	#  Add active bit at the 5th location
	#  00110 => 0+1+0110
	#        => 010110 (22)
	task.appendActiveBit(num, location)
	#  new case
	num = 6
	location = 1
	#  (6) = (110)
	#  Add active bit at the 5th location
	#  110 => 110+1
	#      => 1101 (13)
	task.appendActiveBit(num, location)
end

main()

Output

 Given Number : 17
 Append Location : 3
 Output : 37 

 Given Number : 6
 Append Location : 5
 Output : 22 

 Given Number : 6
 Append Location : 1
 Output : 13 
/*
  Scala program
  Append a set bit in given position of a number
*/
class BitManipulation
{
	// Append a active bits in given valid positions
	def appendActiveBit(num: Int, location: Int): Unit = {
		if (num < 0 || location < 1)
		{
			return;
		}
		// Get the left side part of a append bits in given number
		var left: Int = (num) & ((1 << (location - 1)) - 1);
		// Inactive the all bits of left side part
		var result: Int = num - left;
		// Add Active bit at given location
		result = (result << 1) ^ (1 << location - 1);
		// Add left part
		result = result + left;
		// Display given values
		print("\n Given Number : " + num);
		print("\n Append Location : " + location);
		// Display calculated result
		print("\n Output : " + result + " \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitManipulation = new BitManipulation();
		var num: Int = 17;
		var location: Int = 3;
		// (17) = (10001)
		// Add active bit at the 3rd location
		// 10001 => 100 + 1 + 01
		//       => 100101 (37)
		task.appendActiveBit(num, location);
		// new case
		num = 6;
		location = 5;
		// (6) = (00110)
		// Add active bit at the 5th location
		// 00110 => 0+1+0110
		//       => 010110 (22)
		task.appendActiveBit(num, location);
		// new case
		num = 6;
		location = 1;
		// (6) = (110)
		// Add active bit at the 5th location
		// 110 => 110+1
		//     => 1101 (13)
		task.appendActiveBit(num, location);
	}
}

Output

 Given Number : 17
 Append Location : 3
 Output : 37

 Given Number : 6
 Append Location : 5
 Output : 22

 Given Number : 6
 Append Location : 1
 Output : 13
/*
  Swift 4 program
  Append a set bit in given position of a number
*/
class BitManipulation
{
	// Append a active bits in given valid positions
	func appendActiveBit(_ num: Int, _ location: Int)
	{
		if (num < 0 || location < 1)
		{
			return;
		}
		// Get the left side part of a append bits in given number
		let left: Int = (num) & ((1 << (location - 1)) - 1);
		// Inactive the all bits of left side part
		var result: Int = num - left;
		// Add Active bit at given location
		result = (result << 1) ^ (1 << (location - 1));
		// Add left part
		result = result + left;
		// Display given values
		print("\n Given Number : ", num, terminator: "");
		print("\n Append Location : ", location, terminator: "");
		// Display calculated result
		print("\n Output : ", result ," ");
	}
}
func main()
{
	let task: BitManipulation = BitManipulation();
	var num: Int = 17;
	var location: Int = 3;
	// (17) = (10001)
	// Add active bit at the 3rd location
	// 10001 => 100 + 1 + 01
	//       => 100101 (37)
	task.appendActiveBit(num, location);
	// new case
	num = 6;
	location = 5;
	// (6) = (00110)
	// Add active bit at the 5th location
	// 00110 => 0+1+0110
	//       => 010110 (22)
	task.appendActiveBit(num, location);
	// new case
	num = 6;
	location = 1;
	// (6) = (110)
	// Add active bit at the 5th location
	// 110 => 110+1
	//     => 1101 (13)
	task.appendActiveBit(num, location);
}
main();

Output

 Given Number :  17
 Append Location :  3
 Output :  37

 Given Number :  6
 Append Location :  5
 Output :  22

 Given Number :  6
 Append Location :  1
 Output :  13
/*
  Kotlin program
  Append a set bit in given position of a number
*/
class BitManipulation
{
	// Append a active bits in given valid positions
	fun appendActiveBit(num: Int, location: Int): Unit
	{
		if (num < 0 || location < 1)
		{
			return;
		}
		// Get the left side part of a append bits in given number
		var left: Int = (num) and((1 shl(location - 1)) - 1);
		// Inactive the all bits of left side part
		var result: Int = num - left;
		// Add Active bit at given location
		result = (result shl 1) xor(1 shl location - 1);
		// Add left part
		result = result + left;
		// Display given values
		print("\n Given Number : " + num);
		print("\n Append Location : " + location);
		// Display calculated result
		print("\n Output : " + result + " \n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: BitManipulation = BitManipulation();
	var num: Int = 17;
	var location: Int = 3;
	// (17) = (10001)
	// Add active bit at the 3rd location
	// 10001 => 100 + 1 + 01
	//       => 100101 (37)
	task.appendActiveBit(num, location);
	// new case
	num = 6;
	location = 5;
	// (6) = (00110)
	// Add active bit at the 5th location
	// 00110 => 0+1+0110
	//       => 010110 (22)
	task.appendActiveBit(num, location);
	// new case
	num = 6;
	location = 1;
	// (6) = (110)
	// Add active bit at the 5th location
	// 110 => 110+1
	//     => 1101 (13)
	task.appendActiveBit(num, location);
}

Output

 Given Number : 17
 Append Location : 3
 Output : 37

 Given Number : 6
 Append Location : 5
 Output : 22

 Given Number : 6
 Append Location : 1
 Output : 13

Time Complexity:

The time complexity of the given code is O(1). The operations performed in the appendActiveBit function (bitwise AND, subtraction, left-shift, XOR, and addition) are all constant-time operations that do not depend on the size of the input. Therefore, the time complexity remains constant.





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