Append a set bit in given position of a number
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
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