# Active all the bits in given range of a number

Here given code implementation process.

``````// C Program
// Active all the bits in given range of a number
#include <stdio.h>

// Active all bits in given range from low to high
void activateBits(int num, int low, int high)
{
if (low < 0 || high < 0)
{
// When bit position are not valid
return;
}
else
{
// Display given number and range
printf("\n Given Number : %d", num);
printf("\n Given Ranges : (%d,%d) ", low, high);
// Active all bits in range low to high
int result = (((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) | num;
// Display calculated result
printf("\n Output : %d\n", result);
}
}
int main(int argc, char
const *argv[])
{
// 69 => (1000101)
// (1000101)
//     --- Range (2,4) bit position
// (1001111) Result
activateBits(69, 2, 4);
// 8 => (0001000)
// (0001000)
//  ---     Range (5,7) bit position
// (1111000) Result
activateBits(8, 5, 7);
return 0;
}``````

#### Output

`````` Given Number : 69
Given Ranges : (2,4)
Output : 79

Given Number : 8
Given Ranges : (5,7)
Output : 120``````
``````/*
Java program
Active all the bits in given range of a number
*/
public class Activation
{
// Active all bits in given range from low to high
public void activateBits(int num, int low, int high)
{
if (low < 0 || high < 0)
{
// When bit position are not valid
return;
}
else
{
// Display given number and range
System.out.print("\n Given Number : " + num);
System.out.print("\n Given Ranges : (" + low + "," + high + ") ");
// Active all bits in range low to high
int result = (((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) | num;
// Display calculated result
System.out.print("\n Output : " + result + "\n");
}
}
public static void main(String[] args)
{
// 69 => (1000101)
// (1000101)
//     --- Range (2,4) bit position
// (1001111) Result
// 8 => (0001000)
// (0001000)
//  ---     Range (5,7) bit position
// (1111000) Result
}
}``````

#### Output

`````` Given Number : 69
Given Ranges : (2,4)
Output : 79

Given Number : 8
Given Ranges : (5,7)
Output : 120``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ program
Active all the bits in given range of a number
*/
class Activation
{
public:
// Active all bits in given range from low to high
void activateBits(int num, int low, int high)
{
if (low < 0 || high < 0)
// When bit position are not valid
{
return;
}
else
{
// Display given number and range
cout << "\n Given Number : " << num;
cout << "\n Given Ranges : (" << low << "," << high << ") ";
// Active all bits in range low to high
int result = (((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) | num;
// Display calculated result
cout << "\n Output : " << result << "\n";
}
}
};
int main()
{
// 69 => (1000101)
// (1000101)
//     --- Range (2,4) bit position
// (1001111) Result
// 8 => (0001000)
// (0001000)
//  ---     Range (5,7) bit position
// (1111000) Result
return 0;
}``````

#### Output

`````` Given Number : 69
Given Ranges : (2,4)
Output : 79

Given Number : 8
Given Ranges : (5,7)
Output : 120``````
``````// Include namespace system
using System;
/*
C# program
Active all the bits in given range of a number
*/
public class Activation
{
// Active all bits in given range from low to high
public void activateBits(int num, int low, int high)
{
if (low < 0 || high < 0)
// When bit position are not valid
{
return;
}
else
{
// Display given number and range
Console.Write("\n Given Number : " + num);
Console.Write("\n Given Ranges : (" + low + "," + high + ") ");
// Active all bits in range low to high
int result = (((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) | num;
// Display calculated result
Console.Write("\n Output : " + result + "\n");
}
}
public static void Main(String[] args)
{
// 69 => (1000101)
// (1000101)
//     --- Range (2,4) bit position
// (1001111) Result
// 8 => (0001000)
// (0001000)
//  ---     Range (5,7) bit position
// (1111000) Result
}
}``````

#### Output

`````` Given Number : 69
Given Ranges : (2,4)
Output : 79

Given Number : 8
Given Ranges : (5,7)
Output : 120``````
``````<?php
/*
Php program
Active all the bits in given range of a number
*/
class Activation
{
// Active all bits in given range from low to high
public	function activateBits(\$num, \$low, \$high)
{
if (\$low < 0 || \$high < 0)
// When bit position are not valid
{
return;
}
else
{
// Display given number and range
echo "\n Given Number : ". \$num;
echo "\n Given Ranges : (". \$low .",". \$high .") ";
// Active all bits in range low to high
\$result = (((1 << \$high) - 1) ^ ((1 << (\$low - 1)) - 1)) | \$num;
// Display calculated result
echo "\n Output : ". \$result ."\n";
}
}
}

function main()
{
// 69 => (1000101)
// (1000101)
//     --- Range (2,4) bit position
// (1001111) Result
// 8 => (0001000)
// (0001000)
//  ---     Range (5,7) bit position
// (1111000) Result
}
main();``````

#### Output

`````` Given Number : 69
Given Ranges : (2,4)
Output : 79

Given Number : 8
Given Ranges : (5,7)
Output : 120``````
``````/*
Node Js program
Active all the bits in given range of a number
*/
class Activation
{
// Active all bits in given range from low to high
activateBits(num, low, high)
{
if (low < 0 || high < 0)
// When bit position are not valid
{
return;
}
else
{
// Display given number and range
process.stdout.write("\n Given Number : " + num);
process.stdout.write("\n Given Ranges : (" + low + "," + high + ") ");
// Active all bits in range low to high
var result = (((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) | num;
// Display calculated result
process.stdout.write("\n Output : " + result + "\n");
}
}
}

function main()
{
// 69 => (1000101)
// (1000101)
//     --- Range (2,4) bit position
// (1001111) Result
// 8 => (0001000)
// (0001000)
//  ---     Range (5,7) bit position
// (1111000) Result
}
main();``````

#### Output

`````` Given Number : 69
Given Ranges : (2,4)
Output : 79

Given Number : 8
Given Ranges : (5,7)
Output : 120``````
``````#   Python 3 program
#   Active all the bits in given range of a number

class Activation :
#  Active all bits in given range from low to high
def activateBits(self, num, low, high) :
if (low < 0 or high < 0) :
#  When bit position are not valid
return
else :
#  Display given number and range
print("\n Given Number : ", num, end = "")
print("\n Given Ranges : (", low ,",", high ,") ", end = "")
#  Active all bits in range low to high
result = (((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) | num
#  Display calculated result
print("\n Output : ", result )

def main() :
#  69 => (1000101)
#  (1000101)
#      --- Range (2,4) bit position
#  (1001111) Result
#  8 => (0001000)
#  (0001000)
#   ---     Range (5,7) bit position
#  (1111000) Result

if __name__ == "__main__": main()``````

#### Output

`````` Given Number :  69
Given Ranges : ( 2 , 4 )
Output :  79

Given Number :  8
Given Ranges : ( 5 , 7 )
Output :  120``````
``````#   Ruby program
#   Active all the bits in given range of a number

class Activation
#  Active all bits in given range from low to high
def activateBits(num, low, high)
if (low < 0 || high < 0)
#  When bit position are not valid
return
else
#  Display given number and range
print("\n Given Number : ", num)
print("\n Given Ranges : (", low ,",", high ,") ")
#  Active all bits in range low to high
result = (((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) | num
#  Display calculated result
print("\n Output : ", result ,"\n")
end

end

end

def main()
#  69 => (1000101)
#  (1000101)
#      --- Range (2,4) bit position
#  (1001111) Result
#  8 => (0001000)
#  (0001000)
#   ---     Range (5,7) bit position
#  (1111000) Result
end

main()``````

#### Output

`````` Given Number : 69
Given Ranges : (2,4)
Output : 79

Given Number : 8
Given Ranges : (5,7)
Output : 120
``````
``````/*
Scala program
Active all the bits in given range of a number
*/
class Activation
{
// Active all bits in given range from low to high
def activateBits(num: Int, low: Int, high: Int): Unit = {
if (low < 0 || high < 0)
// When bit position are not valid
{
return;
}
else
{
// Display given number and range
print("\n Given Number : " + num);
print("\n Given Ranges : (" + low + "," + high + ") ");
// Active all bits in range low to high
var result: Int = (((1 << high) - 1) ^ ((1 << (low - 1)) - 1)) | num;
// Display calculated result
print("\n Output : " + result + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Activation = new Activation();
// 69 => (1000101)
// (1000101)
//     --- Range (2,4) bit position
// (1001111) Result
// 8 => (0001000)
// (0001000)
//  ---     Range (5,7) bit position
// (1111000) Result
}
}``````

#### Output

`````` Given Number : 69
Given Ranges : (2,4)
Output : 79

Given Number : 8
Given Ranges : (5,7)
Output : 120``````
``````/*
Swift 4 program
Active all the bits in given range of a number
*/
class Activation
{
// Active all bits in given range from low to high
func activateBits(_ num: Int, _ low: Int, _ high: Int)
{
if (low < 0 || high < 0)
// When bit position are not valid
{
return;
}
else
{
// Display given number and range
print("\n Given Number : ", num, terminator: "");
print("\n Given Ranges : (", low ,",", high ,") ", terminator: "");
// Active all bits in range low to high
let l = ((1 << (low - 1)) - 1);
let result: Int = num | (((1 << high) - 1) ^ l) ;
// Display calculated result
print("\n Output : ", result );
}
}
}
func main()
{
// 69 => (1000101)
// (1000101)
//     --- Range (2,4) bit position
// (1001111) Result
// 8 => (0001000)
// (0001000)
//  ---     Range (5,7) bit position
// (1111000) Result
}
main();``````

#### Output

`````` Given Number :  69
Given Ranges : ( 2 , 4 )
Output :  79

Given Number :  8
Given Ranges : ( 5 , 7 )
Output :  120``````
``````/*
Kotlin program
Active all the bits in given range of a number
*/
class Activation
{
// Active all bits in given range from low to high
fun activateBits(num: Int, low: Int, high: Int): Unit
{
if (low < 0 || high < 0)
// When bit position are not valid
{
return;
}
else
{
// Display given number and range
print("\n Given Number : " + num);
print("\n Given Ranges : (" + low + "," + high + ") ");
// Active all bits in range low to high
var result: Int = (((1 shl high) - 1) xor((1 shl(low - 1)) - 1)) or num;
// Display calculated result
print("\n Output : " + result + "\n");
}
}
}
fun main(args: Array < String > ): Unit
{
// 69 => (1000101)
// (1000101)
//     --- Range (2,4) bit position
// (1001111) Result
// 8 => (0001000)
// (0001000)
//  ---     Range (5,7) bit position
// (1111000) Result
}``````

#### Output

`````` Given Number : 69
Given Ranges : (2,4)
Output : 79

Given Number : 8
Given Ranges : (5,7)
Output : 120``````

## 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.