Set all the bits in given range of a number Last Updated : 27 May, 2022 Summarize Suggest changes Share Like Article Like Report 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 Recommended PracticeSet all the bits in given range of a numberTry It! 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; } 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. 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. 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 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 ?> JavaScript <script> // Javascript 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). let range = (((1 << (l - 1)) - 1) ^ ((1 << (r)) - 1)); return (n | range); } // Driver code let n = 17, l = 2, r = 3; document.write(setallbitgivenrange(n, l, r)); </script> Output : 23 Time Complexity : O(1) Auxiliary Space : O(1) Advertise with us Next Article Smallest number whose set bits are maximum in a given range D Devanshu Agarwal Similar Reads Set the K-th bit of a given number Given a number n and a value k. From the right, set the kth bit in the binary representation of n. The position of LSB(or last bit) is 0, second last bit is 1 and so on. Also, 0 <= k < x, where x is the number of bits in the binary representation of n.Examples: Input : n = 10, k = 2 Output : 1 4 min read Smallest number whose set bits are maximum in a given range Given a positive integer 'l' and 'r'. Find the smallest number 'n' such that l <= n <= r and count of the number of set bits(number of '1's in binary representation) is as maximum as possible. Examples : Input: 1 4Output: 3Explanation:Binary representation from '1' to '4':110 = 0012210 = 01023 9 min read Print bitwise AND set of a number N Given a number N, print all the numbers which are a bitwise AND set of the binary representation of N. Bitwise AND set of a number N is all possible numbers x smaller than or equal N such that N & i is equal to x for some number i. Examples : Input : N = 5Output : 0, 1, 4, 5 Explanation: 0 & 8 min read Set bits in N equals to M in the given range. 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 = 4Output: 9N = 00000001(Considering 8 bits only)M = 1 7 min read Check if all bits of a number are set Given a number n. The problem is to check whether every bit in the binary representation of the given number is set or not. Here 0 <= n.Examples : Input : 7Output : Yes(7)10 = (111)2Input : 14Output : NoMethod 1: If n = 0, then answer is 'No'. Else perform the two operations until n becomes 0. Wh 11 min read Position of the K-th set bit in a number Given two numbers N and K, The task is to find the index of the K-th set bit in the number from the right. Note: Indexing in the binary representation starts from 0 from the right. For example in the binary number "000011", the first set bit is at index 0 from the right, and the second set bit is at 6 min read Article Tags : Misc Bit Magic DSA Bitwise-XOR Practice Tags : Bit MagicMisc Like