We are given two numbers n and m, and two-bit positions, i and j. Insert bits of m into n starting from j to i. We can assume that the bits j through i have enough space to fit all of m. That is, if m = 10011, you can assume that there are at least 5 bits between j and i. You would not, for example, have j = 3 and i = 2, because m could not fully fit between bit 3 and bit 2.

Examples :

Input : n = 1024 m = 19 i = 2 j = 6; Output : n = 1100 Binary representations of input numbers m in binary is (10011)_{2}n in binary is (10000000000)_{2}Binary representations of output number (10000000000)_{2}Input : n = 5 m = 3 i = 1 j = 2 Output : 7

**Algorithm :**

1. Clear the bits j through i in n 2. Shift m so that it lines up with bits j through i 3. Return Bitwise AND of m and n.

The trickiest part is Step 1. How do we clear the bits in n? We can do this with a mask. This mask will have all 1s, except for 0s in the bits j through i. We create this mask by creating the left half of the mask first, and then the right half.

Following is the implementation of the above approach.

## C++

`// C++ program for implementation of updateBits() ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to updateBits M insert to N. ` `int` `updateBits(` `int` `n, ` `int` `m, ` `int` `i, ` `int` `j) ` `{ ` ` ` `/* Create a mask to clear bits i through j ` ` ` `in n. EXAMPLE: i = 2, j = 4. Result ` ` ` `should be 11100011. For simplicity, we'll ` ` ` `use just 8 bits for the example. */` ` ` ` ` `int` `allOnes = ~0; ` `// will equal sequence of all ls ` ` ` ` ` `// ls before position j, then 0s. left = 11100000 ` ` ` `int` `left= allOnes << (j + 1); ` ` ` ` ` `// l's after position i. right = 00000011 ` ` ` `int` `right = ((1 << i) - 1); ` ` ` ` ` `// All ls, except for 0s between i and j. mask 11100011 ` ` ` `int` `mask = left | right; ` ` ` ` ` `/* Clear bits j through i then put min there */` ` ` `int` `n_cleared = n & mask; ` `// Clear bits j through i. ` ` ` `int` `m_shifted = m << i; ` `// Move m into correct position. ` ` ` ` ` `return` `(n_cleared | m_shifted); ` `// OR them, and we're done! ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 1024; ` `// in Binary N= 10000000000 ` ` ` `int` `m = 19; ` `// in Binary M= 10011 ` ` ` `int` `i = 2, j = 6; ` ` ` ` ` `cout << updateBits(n,m,i,j); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for implementation of updateBits() ` ` ` `class` `UpdateBits ` `{ ` ` ` `// Function to updateBits M insert to N. ` ` ` `static` `int` `updateBits(` `int` `n, ` `int` `m, ` `int` `i, ` `int` `j) ` ` ` `{ ` ` ` `/* Create a mask to clear bits i through j ` ` ` `in n. EXAMPLE: i = 2, j = 4. Result ` ` ` `should be 11100011. For simplicity, we'll ` ` ` `use just 8 bits for the example. */` ` ` ` ` `int` `allOnes = ~` `0` `; ` `// will equal sequence of all ls ` ` ` ` ` `// ls before position j, then 0s. left = 11100000 ` ` ` `int` `left= allOnes << (j + ` `1` `); ` ` ` ` ` `// l's after position i. right = 00000011 ` ` ` `int` `right = ((` `1` `<< i) - ` `1` `); ` ` ` ` ` `// All ls, except for 0s between i and j. mask 11100011 ` ` ` `int` `mask = left | right; ` ` ` ` ` `/* Clear bits j through i then put min there */` ` ` `// Clear bits j through i. ` ` ` `int` `n_cleared = n & mask; ` ` ` `// Move m into correct position. ` ` ` `int` `m_shifted = m << i; ` ` ` ` ` `// OR them, and we're done! ` ` ` `return` `(n_cleared | m_shifted); ` ` ` `} ` ` ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `// in Binary N= 10000000000 ` ` ` `int` `n = ` `1024` `; ` ` ` ` ` `// in Binary M= 10011 ` ` ` `int` `m = ` `19` `; ` ` ` ` ` `int` `i = ` `2` `, j = ` `6` `; ` ` ` ` ` `System.out.println(updateBits(n,m,i,j)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for implementation ` `# of updateBits() ` ` ` `# Function to updateBits M insert to N. ` `def` `updateBits(n, m, i, j): ` ` ` ` ` `# Create a mask to clear bits i through ` ` ` `# j in n. EXAMPLE: i = 2, j = 4. Result ` ` ` `# should be 11100011. For simplicity, ` ` ` `# we'll use just 8 bits for the example. ` ` ` ` ` `# will equal sequence of all ls ` ` ` `allOnes ` `=` `~` `0` ` ` ` ` `# ls before position j, ` ` ` `# then 0s. left = 11100000 ` ` ` `left ` `=` `allOnes << (j ` `+` `1` `) ` ` ` ` ` `# l's after position i. right = 00000011 ` ` ` `right ` `=` `((` `1` `<< i) ` `-` `1` `) ` ` ` ` ` `# All ls, except for 0s between ` ` ` `# i and j. mask 11100011 ` ` ` `mask ` `=` `left | right ` ` ` ` ` `# Clear bits j through i then put min there ` ` ` `n_cleared ` `=` `n & mask ` ` ` ` ` `# Move m into correct position. ` ` ` `m_shifted ` `=` `m << i ` ` ` ` ` `return` `(n_cleared | m_shifted) ` ` ` ` ` `# Driver Code ` `n ` `=` `1024` `# in Binary N = 10000000000 ` `m ` `=` `19` `# in Binary M = 10011 ` `i ` `=` `2` `; j ` `=` `6` `print` `(updateBits(n, m, i, j)) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program for implementation of ` `// updateBits() ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to updateBits M ` ` ` `// insert to N. ` ` ` `static` `int` `updateBits(` `int` `n, ` `int` `m, ` ` ` `int` `i, ` `int` `j) ` ` ` `{ ` ` ` ` ` `/* Create a mask to clear bits i ` ` ` `through j in n. EXAMPLE: i = 2, ` ` ` `j = 4. Result should be 11100011. ` ` ` `For simplicity, we'll use just 8 ` ` ` `bits for the example. */` ` ` ` ` `// will equal sequence of all ls ` ` ` `int` `allOnes = ~0; ` ` ` ` ` `// ls before position j, then 0s. ` ` ` `// left = 11100000 ` ` ` `int` `left= allOnes << (j + 1); ` ` ` ` ` `// l's after position i. ` ` ` `// right = 00000011 ` ` ` `int` `right = ((1 << i) - 1); ` ` ` ` ` `// All ls, except for 0s between i ` ` ` `// and j. mask 11100011 ` ` ` `int` `mask = left | right; ` ` ` ` ` `/* Clear bits j through i then put ` ` ` `min there */` ` ` `// Clear bits j through i. ` ` ` `int` `n_cleared = n & mask; ` ` ` ` ` `// Move m into correct position. ` ` ` `int` `m_shifted = m << i; ` ` ` ` ` `// OR them, and we're done! ` ` ` `return` `(n_cleared | m_shifted); ` ` ` `} ` ` ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `// in Binary N= 10000000000 ` ` ` `int` `n = 1024; ` ` ` ` ` `// in Binary M= 10011 ` ` ` `int` `m = 19; ` ` ` `int` `i = 2, j = 6; ` ` ` ` ` `Console.WriteLine(updateBits(n, m, i, j)); ` ` ` `} ` `} ` ` ` `//This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for implementation ` `// of updateBits() ` ` ` `// Function to updateBits ` `// M insert to N. ` ` ` `function` `updateBits(` `$n` `, ` `$m` `, ` `$i` `, ` `$j` `) ` `{ ` ` ` `// Create a mask to clear ` ` ` `// bits i through j in n. ` ` ` `// EXAMPLE: i = 2, j = 4. ` ` ` `// Result should be 11100011. ` ` ` `// For simplicity, we'll use ` ` ` `// just 8 bits for the example. ` ` ` ` ` `// will equal sequence of all ls ` ` ` `$allOnes` `= ~0; ` ` ` ` ` `// ls before position j, then ` ` ` `// 0s. left = 11100000 ` ` ` `$left` `= ` `$allOnes` `<< (` `$j` `+ 1); ` ` ` ` ` `// l's after position i. ` ` ` `// right = 00000011 ` ` ` `$right` `= ((1 << ` `$i` `) - 1); ` ` ` ` ` `// All ls, except for 0s between ` ` ` `// i and j. mask 11100011 ` ` ` `$mask` `= ` `$left` `| ` `$right` `; ` ` ` ` ` `// Clear bits j through i ` ` ` `// then put min there ` ` ` ` ` `// Clear bits j through i. ` ` ` `$n_cleared` `= ` `$n` `& ` `$mask` `; ` ` ` ` ` `// Move m into correct position. ` ` ` `$m_shifted` `= ` `$m` `<< ` `$i` `; ` ` ` ` ` `// OR them, and we're done! ` ` ` `return` `(` `$n_cleared` `| ` `$m_shifted` `); ` `} ` ` ` `// Driver Code ` ` ` `// in Binary N= 10000000000 ` `$n` `= 1024; ` ` ` `// in Binary M= 10011 ` `$m` `= 19; ` `$i` `= 2; ` `$j` `= 6; ` ` ` `echo` `updateBits(` `$n` `, ` `$m` `, ` `$i` `, ` `$j` `); ` ` ` `// This code is contributed by Ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

1100 // in Binary (10001001100)_{2}

This article is contributed by **Mr. Somesh Awasthi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Check whether the binary equivalent of a number ends with "001" or not
- Maximize XOR subsequence possible by equidistant elements from both ends
- Check whether the binary equivalent of a number ends with given string or not
- Largest number M having bit count of N such that difference between their OR and XOR value is maximized
- Maximize the expression (A AND X) * (B AND X) | Bit Manipulation
- Convert numbers into binary representation and add them without carry
- Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1
- Check if left and right shift of any string results into given string
- Partition of a set into K subsets with equal sum using BitMask and DP
- Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes
- Set, Clear and Toggle a given bit of a number in C
- Count of even and odd set bit with array element after XOR with K
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Count of even and odd set bit Array elements after XOR with K for Q queries
- Absolute difference between set and unset bit count in N
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1