You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a substring of N located at i and starting at j).

**Examples :**

Input : N = 1, M = 2, i = 2, j = 4 Output: 9 N = 00000001(Considering 8 bits only) M = 10 (Binary of 2) For more indexes, leading zeroes will be considered. Now set 3 bits from ith index to j in the N as in the M. Bits:- 0 0 0 (0 1 0) 0 1 = 9 Indexes:- 7 6 5 4 3 2 1 0 From index 2 to 4, bits are set according to the M.

Asked in : Adobe

A simple solution is to traverse all bits in N from 0 to 31 and set the bits equals to M in the range from i to j.

An efficient solution is to do following steps.

- Set all the bits after j in a number.
- Set all the bits before i in a number.
- Then perform Bitwise Or on both then we get the number with all the bits set except from i to j.
- Perform Bitwise And with the given N as to set the bits according to the N.
- Then shift M into the correct position i.e. in the range of i to j.
- And at the last perform Bitwise Or on (Shifted M and the N modifed in 4th step).
- The result will be N with M as substring from ith to jth bits

## C++

`// C++ program for above implementation ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to set the bits ` `int` `setBits(` `int` `n, ` `int` `m, ` `int` `i, ` `int` `j) ` `{ ` ` ` `// number with all 1's ` ` ` `int` `allOnes = ~0; ` ` ` ` ` `// Set all the bits in the left of j ` ` ` `int` `left = allOnes << (j + 1); ` ` ` ` ` `// Set all the bits in the right of j ` ` ` `int` `right = ((1 << i) - 1); ` ` ` ` ` `// Do Bitwsie OR to get all the bits ` ` ` `// set except in the range from i to j ` ` ` `int` `mask = left | right; ` ` ` ` ` `// clear bits j through i ` ` ` `int` `masked_n = n & mask; ` ` ` ` ` `// move m into the correct position ` ` ` `int` `m_shifted = m << i; ` ` ` ` ` `// return the Bitwise OR of masked_n ` ` ` `// and shifted_m ` ` ` `return` `(masked_n | m_shifted); ` `} ` ` ` `// Drivers program ` `int` `main() ` `{ ` ` ` `int` `n = 2, m = 4; ` ` ` `int` `i = 2, j = 4; ` ` ` `cout << setBits(n, m, i, j); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program ` `public` `class` `GFG ` `{ ` ` ` `// Function to set the bits ` ` ` `static` `int` `setBits(` `int` `n, ` `int` `m, ` `int` `i, ` `int` `j) ` ` ` `{ ` ` ` ` ` `// number with all 1's ` ` ` `int` `allOnes = ~` `0` `; ` ` ` ` ` `// Set all the bits in the left of j ` ` ` `int` `left = allOnes << (j + ` `1` `); ` ` ` ` ` `// Set all the bits in the right of j ` ` ` `int` `right = ((` `1` `<< i) - ` `1` `); ` ` ` ` ` `// Do Bitwise OR to get all the bits ` ` ` `// set except in the range from i to j ` ` ` `int` `mask = left | right; ` ` ` ` ` `// clear bits j through i ` ` ` `int` `masked_n = n & mask; ` ` ` ` ` `// move m into the correct position ` ` ` `int` `m_shifted = m << i; ` ` ` ` ` `// return the Bitwise OR of masked_n ` ` ` `// and shifted_m ` ` ` `return` `(masked_n | m_shifted); ` ` ` `} ` ` ` ` ` `// Driver Program to test above function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `2` `, m = ` `4` `; ` ` ` `int` `i = ` `2` `, j = ` `4` `; ` ` ` `System.out.println(setBits(n, m, i, j)); ` ` ` `} ` `} ` `// This code is contributed by Sumit Ghosh ` |

*chevron_right*

*filter_none*

## Python

`# Python program for above implementation ` ` ` `# Function to set the bits ` `def` `setBits(n, m, i, j): ` ` ` ` ` `# number with all 1's ` ` ` `allOnes ` `=` `not` `0` ` ` ` ` `# Set all the bits in the left of j ` ` ` `left ` `=` `allOnes << (j ` `+` `1` `) ` ` ` ` ` `# Set all the bits in the right of j ` ` ` `right ` `=` `((` `1` `<< i) ` `-` `1` `) ` ` ` ` ` `# Do Bitwsie OR to get all the bits ` ` ` `# set except in the range from i to j ` ` ` `mask ` `=` `left | right ` ` ` ` ` `# clear bits j through i ` ` ` `masked_n ` `=` `n & mask ` ` ` ` ` `# move m into the correct position ` ` ` `m_shifted ` `=` `m << i ` ` ` ` ` `# return the Bitwise OR of masked_n ` ` ` `# and shifted_m ` ` ` `return` `(masked_n | m_shifted) ` ` ` `# Drivers program ` `n, m ` `=` `2` `, ` `4` `i, j ` `=` `2` `, ` `4` `print` `setBits(n, m, i, j) ` ` ` `# This code is submitted by Sachin Bisht ` |

*chevron_right*

*filter_none*

## C#

`// C# Program for above implementation ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// Function to set the bits ` ` ` `static` `int` `setBits(` `int` `n, ` `int` `m, ` `int` `i, ` `int` `j) ` ` ` `{ ` ` ` ` ` `// number with all 1's ` ` ` `int` `allOnes = ~0; ` ` ` ` ` `// Set all the bits in the left of j ` ` ` `int` `left = allOnes << (j + 1); ` ` ` ` ` `// Set all the bits in the right of j ` ` ` `int` `right = ((1 << i) - 1); ` ` ` ` ` `// Do Bitwise OR to get all the bits ` ` ` `// set except in the range from i to j ` ` ` `int` `mask = left | right; ` ` ` ` ` `// clear bits j through i ` ` ` `int` `masked_n = n & mask; ` ` ` ` ` `// move m into the correct position ` ` ` `int` `m_shifted = m << i; ` ` ` ` ` `// return the Bitwise OR of masked_n ` ` ` `// and shifted_m ` ` ` `return` `(masked_n | m_shifted); ` ` ` `} ` ` ` ` ` `// Driver Program to test above function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 2, m = 4; ` ` ` `int` `i = 2, j = 4; ` ` ` ` ` `Console.WriteLine(setBits(n, m, i, j)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for above implementation ` ` ` `// Function to set the bits ` `function` `setBits(` `$n` `, ` `$m` `, ` `$i` `, ` `$j` `) ` `{ ` ` ` `// number with all 1's ` ` ` `$allOnes` `= ~0; ` ` ` ` ` `// Set all the bits ` ` ` `// in the left of j ` ` ` `$left` `= ` `$allOnes` `<< (` `$j` `+ 1); ` ` ` ` ` `// Set all the bits ` ` ` `// in the right of j ` ` ` `$right` `= ((1 << ` `$i` `) - 1); ` ` ` ` ` `// Do Bitwsie OR to get all ` ` ` `// the bits set except in ` ` ` `// the range from i to j ` ` ` `$mask` `= ` `$left` `| ` `$right` `; ` ` ` ` ` `// clear bits j through i ` ` ` `$masked_n` `= ` `$n` `& ` `$mask` `; ` ` ` ` ` `// move m into the ` ` ` `// correct position ` ` ` `$m_shifted` `= ` `$m` `<< ` `$i` `; ` ` ` ` ` `// return the Bitwise OR ` ` ` `// of masked_n and shifted_m ` ` ` `return` `(` `$masked_n` `| ` `$m_shifted` `); ` `} ` ` ` `// Driver Code ` `$n` `= 2; ` `$m` `= 4; ` `$i` `= 2; ` `$j` `= 4; ` `echo` `setBits(` `$n` `, ` `$m` `, ` `$i` `, ` `$j` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

18

Reference:

https://www.careercup.com/question?id=8863294

This article is contributed by **Sahil Chhabra**. 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:

- Check if bits of a number has count of consecutive set bits in increasing order
- Print numbers having first and last bits as the only set bits
- Check whether all the bits are set in the given range
- Set all the bits in given range of a number
- Smallest number whose set bits are maximum in a given range
- Check whether bits are in alternate pattern in the given range | Set-2
- Toggle bits of a number except first and last bits
- Check if all bits can be made same by flipping two consecutive bits
- Copy set bits in a range
- Count set bits in a range
- Python | Count set bits in a range
- Range query for count of set bits
- Count number of set bits in a range using bitset
- Count total set bits in all numbers from range L to R
- Toggle bits in the given range
- Unset bits in the given range
- Check whether all the bits are unset in the given range or not
- Check whether all the bits are unset in the given range
- Check whether bits are in alternate pattern in the given range
- Count unset bits in a range