- Related Questions & Answers
- Position of the K-th set bit in a number in C++
- Gets or sets the value of the bit at a specific position in the BitArray in C#
- Check whether the bit at given position is set or unset in Python
- Find position of the only set bit in C++
- Golang program to update the ith index node value, when index is at the nth position, i.e., out of index bound.
- Find the index of the first unique character in a given string using C++
- Get value of the bit at a specific position in BitArray in C#
- Position of rightmost set bit in C++
- Position of rightmost different bit in C++
- Program to find number of bit 1 in the given number in Python
- Find position of the given number among the numbers made of 4 and 7 in C++
- 8085 program to count number of once in the given 8-bit number
- 8085 program to check whether the given 16 bit number is palindrome or not
- Golang Program to find the position of the rightmost set bit
- Set the bit at a specific position in the BitArray to the specified value in C#?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In the given problem, we have to update the bit of the given index of a number. To update the number, we can use Bit Manipulation operation on a given number. For example,

**Input-1** −

N= 25 bit= 1 position= 2

**Output** −

29

**Explanation** − Since the given input 25 can be written in binary as ‘11001’ whereas the position index is ‘2’ and the bit is ‘1’. After replacing the digits at the given position the output will be ‘11101’ which is equivalent to ‘29’.

In the given position or index of a number, the task is to update the bit with the particular bit given in the input. The approach to update the bit at the given position is first to clear the bit at the given position and then perform a Binary AND operation to update the bit.

Take Input a number N, bit to update as ‘bit’ and position or index on which we have to update the bit as ‘position’.

A void function updateBit(int &n, int bit, int position) takes the address of the current bit, bit value and the index of the bit. The function will print the updated value of the number by replacing the bit with the given one.

Clear the bit at the given position and add the resultant bit to the result.

Create a mask and perform an AND operation with the result.

Perform Binary OR operation with the mask we have created and perform the right shift operation with the value to be updated at the index.

#include<iostream> using namespace std; void updateBit(int &n,int bit, int pos){ int clearBit= ~(1<<pos); int mask= n & clearBit; n= mask |(bit<<pos); } int main(){ int n=25; int bit=1; int pos=2; updateBit(n,bit,pos); cout<<n; }

Running the above code will generate the output as,

29

Since the input is 25 which is 11001 in binary representation. After replacing the position which is ‘2’ with ‘1’, it will become 11101 in binary which is 29.

Advertisements