Posted on by Kalkicode
Code Bit Logic

# Active all the bits in given range of a number

In this article, we will discuss a program to activate all the bits within a specified range of a given number. The goal is to manipulate the bits in such a way that all bits within the range are set to 1 while keeping the remaining bits unchanged. We will provide a suitable example to demonstrate the working of the program and explain the algorithm used for this task. Additionally, we will analyze the time complexity of the algorithm to understand its efficiency.

## Problem Statement

Given a positive integer 'num' and two non-negative integers 'low' and 'high', the task is to activate all the bits within the range 'low' to 'high' (inclusive) in the binary representation of 'num'. The range is defined by the bit positions 'low' and 'high'. The program should return the modified number with the specified bits activated.

## Example

Let's consider two examples to understand the concept better.

## Example 1

```Input: num = 69 (1000101 in binary), low = 2, high = 4
Output: 79 (1001111 in binary)```

## Example 2

```Input: num = 8 (0001000 in binary), low = 5, high = 7
Output: 120 (1111000 in binary)```

## Pseudocode

1. Start the program.
2. Define a function 'activateBits' with parameters 'num', 'low', and 'high'.
3. Check if 'low' or 'high' is negative, if so, exit the function.
4. Calculate the mask to activate the bits in the range (low to high).
5. Perform bitwise OR operation with the mask and the original number 'num'.
6. Print the given number, range, and the calculated output.
7. End the function.
8. In the 'main' function, call 'activateBits' with the given examples as input.
9. End the program.

## Algorithm

1. Check if 'low' or 'high' is negative, if true, return from the function.
2. Calculate the mask to activate the bits within the range using bitwise operations: a. Create a mask for the 'high' bits by left-shifting 1 by 'high' positions and subtracting 1. b. Create a mask for the 'low' bits by left-shifting 1 by ('low' - 1) positions and subtracting 1. c. XOR the 'high' mask with the 'low' mask to create the final mask for the range.
3. Perform a bitwise OR operation between the calculated mask and the given number 'num'.
4. Return the result.

## Explanation of Output

In Example 1, the binary representation of 69 is 1000101. The range to be activated is from bit position 2 to 4 (counting from the rightmost bit as 1). After activating the bits within the range, the resulting binary is 1001111, which is equivalent to 79 in decimal.

In Example 2, the binary representation of 8 is 0001000. The range to be activated is from bit position 5 to 7. After activating the bits within the range, the resulting binary is 1111000, which is equivalent to 120 in decimal.

## Code Solution

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``````

## Time Complexity

The time complexity of the algorithm is O(1) because the operations performed are constant and do not depend on the size of the number or the range. The bitwise operations involve a fixed number of steps, making the algorithm efficient and independent of the input size.

## Finally

In this article, we discussed a program to activate all the bits within a specified range of a given number. We explained the problem statement, provided suitable examples, presented the pseudocode, and detailed the algorithm used to achieve the desired functionality. Additionally, we analyzed the time complexity of the algorithm, which is constant and efficient for practical purposes. By understanding the concept of bitwise operations, readers can apply similar techniques to solve other problems related to bit manipulation.

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

Categories
Relative Post