How to Turn on a Particular Bit in a Number?


The problem statement says that we need to turn on a particular bit in a number. To put it simply, all we have to do is swap out a certain bit in a number for 1. Provide the same outcome if it has already been 1.

You can represent an integer using binary numbers, which come in the forms of 0 and 1 where every digit represents a power of 2.

Let's represent 5 in the binary numbers.

The binary form of 5 will be 101 i.e.$\mathrm{2^{2} + 2^{0}= 5.}$

In this problem, a number N will be given as an input and a number A. Our task will be to change the Ath bit in the number N with 1. If the Ath bit is already 1 in N, remain it unchanged. This is how we are going to turn on a Ath bit in a number.

For example,

Input

N=10 , A=3

Output

14

Explanation : The binary representation of 10 is 1010. So changing the Ath bit i.e. 3rd bit to 1 will make it 1110. This is the binary representation of the number,

$\mathrm{2^{3}+2^{2}+2^{1}=14}$

Input

N=18 , A=6

Output

50

Explanation : 18 is written as 10010 in the binary form. Now, turning on the 6th bit will make it 110010. The decimal value of this binary number will be 50().

$\mathrm{2^{5}+2^{4}+2^{1}=50}$

Let us look at the algorithm to change a particular bit in the given number with 1.

Algorithm

This problem can be solved using bit manipulation with the help of bitwise operators. If we see the problem statement we just need to change the Ath bit with 1 which can be done using the OR operator. The OR operator returns 0 if both the bits are 0 and 1 if any of the two bits is 1.

If we can get a number whose Ath bit is 1 and all other bits are 0. We can do the bitwise | (OR) of both the numbers to get the required output as it will change that bit to 1 if it is 0 or remain unchanged if it is 1.

We can understand the above concept with an example:

Let's assume we are given N=8 and A=3 which means we need to change the 3rd bit to 1.

To get the number whose 3rd bit is 1 and all other bits are 0 we can use the left shift operator. This operator moves the bit of the number to the left.

Syntax for left shift operator:

int a=1;
a=a<<2; //left shift a by 2

Initially a equals to 1 which can be represented as 00001 in the binary form. After left shifting it by 2, it moves all the bits of the number by 2.

After that operation, a becomes 00100.

In this example, to get 1 at the 3rd bit we need to left shift 1 by 2.

Similarly, we can get the 1 at Ath bit and remaining bits 0 by left shift 1 by (A-1).

Applying the OR operator on N and (1<<(A-1)), we can change the Ath bit of the number with 1 without changing the remaining bits.

Approach

Following the below steps we can implement the above algorithm:

  • First we need to check if A is greater than 0. If A is less than or equal to zero return the number N.

  • If A>0, we need to change the Ath bit of the number.

  • Simply left shift 1 by A-1 to get 1 at the Ath bit and keep the remaining bits 0.

  • Then use the OR operator on the number N and (1<<(A-1)) to get the number after changing the Ath bit of the number N, which is our required output.

The implementation of this approach in C++:

Example

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

//function to turn on Ath bit in number N
int change_bit(int N, int A){
   
   if(A<=0){  //if the position of bit which is to be changed is less than or equal to 0, return N
      return N; 
   }
   
   int m=1<<(A-1); //to get 1 at Ath bit by using left shift operator
   int num= N | m;  //using OR operation to change the Ath bit with 1
   
   return num; //return the number after changing Ath bit in number N
}

int main()
{
   int N,A;
   
   N=24,A=3;
   cout<<"The number after turning on "<<A<<"th bit in number "<<N<<" is : "<<change_bit(N,A)<<endl;
   
   N=55,A=8;
   cout<<"The number after turning on "<<A<<"th bit in number "<<N<<" is : "<<change_bit(N,A)<<endl;
   

   return 0;
}

Output

The number after turning on 3th bit in number 24 is : 28
The number after turning on 8th bit in number 55 is : 183

Time Complexity : O(1) , since constant time is required to perform the operation

Space Complexity : O(1) , since no extra space is used.

Conclusion

The algorithm to activate a specific bit in a number N with 1 was covered in this article. By manipulating bits with the help of C++'s built-in bitwise operators, we developed an effective method that has constant time complexity.

In relation to the issue, I believe this essay can help you understand all of your notions.

Updated on: 21-Aug-2023

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements