Given a non-negative number n and two values l and r. The problem is to set the bits in the range l to r in the binary representation of n, i.e, to unset bits from the rightmost lth bit to the rightmost r-th bit.

Constraint: 1 <= l <= r <= number of bits in the binary representation of n.

**Examples :**

Input : n = 17, l = 2, r = 3 Output : 23 (17)_{10}= (10001)_{2}(23)_{10}= (10111)_{2}The bits in the range 2 to 3 in the binary representation of 17 are set. Input : n = 50, l = 2, r = 5 Output : 62

Approach: Following are the steps:

1. Find a number 'range' that has all set bits in given range. And all other bits of this number are 0. range = (((1 << (l - 1)) - 1) ^ ((1 << (r)) - 1)); 2. Now, perform "n = n | range". This will set the bits in the range from l to r in n.

## C++

`// C++ implementation to Set bits in ` `// the given range ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// function to toggle bits in the given range ` `int` `setallbitgivenrange(` `int` `n, ` `int` `l, ` `int` `r) ` `{ ` ` ` `// calculating a number 'range' having set ` ` ` `// bits in the range from l to r and all other ` ` ` `// bits as 0 (or unset). ` ` ` `int` `range = (((1 << (l - 1)) - 1) ^ ` ` ` `((1 << (r)) - 1)); ` ` ` ` ` `return` `(n | range); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 17, l = 2, r = 3; ` ` ` `cout << setallbitgivenrange(n, l, r); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java implementation to Set bits in ` `// the given range ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// function to toggle bits in the ` ` ` `// given range ` ` ` `static` `int` `setallbitgivenrange(` `int` `n, ` ` ` `int` `l, ` `int` `r) ` ` ` `{ ` ` ` ` ` `// calculating a number 'range' ` ` ` `// having set bits in the range ` ` ` `// from l to r and all other ` ` ` `// bits as 0 (or unset). ` ` ` `int` `range = (((` `1` `<< (l - ` `1` `)) - ` `1` `) ^ ` ` ` `((` `1` `<< (r)) - ` `1` `)); ` ` ` ` ` `return` `(n | range); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `17` `, l = ` `2` `, r = ` `3` `; ` ` ` ` ` `System.out.println(setallbitgivenrange( ` ` ` `n, l, r)); ` ` ` `} ` ` ` `} ` ` ` `// This code is contributed by Sam007. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to Set ` `# bits in the given range ` ` ` `# Function to toggle bits ` `# in the given range ` `def` `setallbitgivenrange(n, l, r): ` ` ` ` ` `# calculating a number 'range' ` ` ` `# having set bits in the range ` ` ` `# from l to r and all other ` ` ` `# bits as 0 (or unset). ` ` ` `range` `=` `(((` `1` `<< (l ` `-` `1` `)) ` `-` `1` `) ^ ` ` ` `((` `1` `<< (r)) ` `-` `1` `)) ` ` ` ` ` `return` `(n | ` `range` `) ` ` ` `# Driver code ` `n, l, r ` `=` `17` `, ` `2` `, ` `3` `print` `(setallbitgivenrange(n, l, r)) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to Set ` `// bits in the given range ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// function to toggle bits ` ` ` `// in the given range ` ` ` `static` `int` `setallbitgivenrange(` `int` `n, ` `int` `l, ` `int` `r) ` ` ` `{ ` ` ` ` ` `// calculating a number 'range' ` ` ` `// having set bits in the range ` ` ` `// from l to r and all other ` ` ` `// bits as 0 (or unset). ` ` ` `int` `range = (((1 << (l - 1)) - 1) ^ ` ` ` `((1 << (r)) - 1)); ` ` ` ` ` `return` `(n | range); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 17, l = 2, r = 3; ` ` ` `Console.Write(setallbitgivenrange(n, l, r)); ` ` ` `} ` ` ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation to Set ` `// bits in the given range ` ` ` `// function to toggle bits ` `// in the given range ` `function` `setallbitgivenrange(` `$n` `, ` `$l` `, ` `$r` `) ` `{ ` ` ` `// calculating a number 'range' ` ` ` `// having set bits in the range ` ` ` `// from l to r and all other ` ` ` `// bits as 0 (or unset). ` ` ` `$range` `= (((1 << (` `$l` `- 1)) - 1) ^ ` ` ` `((1 << (` `$r` `)) - 1)); ` ` ` ` ` `return` `(` `$n` `| ` `$range` `); ` `} ` ` ` `// Driver code ` `$n` `= 17; ` `$l` `= 2; ` `$r` `= 3; ` `echo` `setallbitgivenrange(` `$n` `, ` `$l` `, ` `$r` `); ` ` ` `// This code is contributed by Sam007 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

23

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

- Count number of set bits in a range using bitset
- Smallest number whose set bits are maximum in a given range
- Check if bits of a number has count of consecutive set bits in increasing order
- Set bits in N equals to M in the given range.
- Unset bits in the given range
- Copy set bits in a range
- Toggle bits in the given range
- Count set bits in a range
- Check whether all the bits are set in the given range
- Python | Count set bits in a range
- Range query for count of set bits
- Count unset bits in a range
- Check whether all the bits are unset in the given range
- Check whether all the bits are unset in the given range or not
- Check if bits in range L to R of two numbers are complement of each other or not
- Count total set bits in all numbers from range L to R
- Check whether bits are in alternate pattern in the given range
- Check whether bits are in alternate pattern in the given range | Set-2
- Python | Count unset bits in a range
- Print numbers in the range 1 to n having bits in alternate pattern