Set Bits Between Given Positions

Last Updated : 21 Jul, 2026

Given a non-negative integer n and two integers l and r, set all the bits from position l to r (both inclusive) in the binary representation of n. Bit positions are numbered starting from 1 for the least significant bit. Return the resulting number.

Examples:

Input: n = 17, l = 2, r = 3
Output: 23
Explanation: 17 = (10001). After setting bits from position 2 to 3, the number becomes (10111) = 23.

Input: n = 8, l = 1, r = 2
Output: 11
Explanation: 8 = (1000). After setting bits from position 1 to 2, the number becomes (1011) = 11.

Try It Yourself
redirect icon

[Naive Approach] Iterate Through the Range - O(r - l + 1) Time and O(1) Space

The idea is to set every bit one by one from position l to r. For each position, create a mask having only that bit set and perform a bitwise OR with the given number. Since OR with 1 always sets the corresponding bit, repeating this for every position gives the required result.

Working of Approach:

  • Traverse all bit positions from l to r.
  • Create a mask for the current bit using 1 << (i - 1).
  • Set the bit by performing n |= mask.
  • Return the modified number.
C++
#include <iostream>
using namespace std;

int setAllRangeBits(int n, int l, int r) {
    for (int i = l; i <= r; i++) {
        n |= (1 << (i - 1));
    }
    return n;
}

int main() {
    int n = 17, l = 2, r = 3;
    cout << setAllRangeBits(n, l, r);
    return 0;
}
Java
class GFG {

    static int setAllRangeBits(int n, int l, int r) {
        for (int i = l; i <= r; i++) {
            n |= (1 << (i - 1));
        }
        return n;
    }

    public static void main(String[] args) {
        int n = 17, l = 2, r = 3;
        System.out.println(setAllRangeBits(n, l, r));
    }
}
Python
def setAllRangeBits(n, l, r):
    for i in range(l, r + 1):
        n |= (1 << (i - 1))
    return n

if __name__ == "__main__":
    n, l, r = 17, 2, 3
    print(setAllRangeBits(n, l, r))
C#
using System;

class GFG {

    static int setAllRangeBits(int n, int l, int r) {
        for (int i = l; i <= r; i++) {
            n |= (1 << (i - 1));
        }
        return n;
    }

    static void Main() {
        int n = 17, l = 2, r = 3;
        Console.WriteLine(setAllRangeBits(n, l, r));
    }
}
JavaScript
function setAllRangeBits(n, l, r) {
    for (let i = l; i <= r; i++) {
        n |= (1 << (i - 1));
    }
    return n;
}

// Driver Code
let n = 17, l = 2, r = 3;
console.log(setAllRangeBits(n, l, r));

Output
23

[Expected Approach] Using Bit Manipulation Mask - O(1) Time and O(1) Space

The idea is to create a bitmask having all bits from position l to r set and all other bits unset. Performing a bitwise OR of this mask with the given number sets every bit in the required range in a single operation.

Working of Approach:

  • Find a number 'mask' that has all set bits in given range. And all other bits of this number are 0. mask = (((1 << (l - 1)) - 1) ^ ((1 << (r)) - 1));
  • Now, perform "n = n | mask". This will set the bits in the range from l to r in n.
C++
#include <iostream>
using namespace std;

int setAllRangeBits(int n, int l, int r) {
    int mask = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
    return n | mask;
}

int main() {
    int n = 17, l = 2, r = 3;
    cout << setAllRangeBits(n, l, r);
    return 0;
}
Java
class GFG {

    static int setAllRangeBits(int n, int l, int r) {
        int mask = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
        return n | mask;
    }

    public static void main(String[] args) {
        int n = 17, l = 2, r = 3;
        System.out.println(setAllRangeBits(n, l, r));
    }
}
Python
def setAllRangeBits(n, l, r):
    mask = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1)
    return n | mask

if __name__ == "__main__":
    n, l, r = 17, 2, 3
    print(setAllRangeBits(n, l, r))
C#
using System;

class GFG {

    static int setAllRangeBits(int n, int l, int r) {
        int mask = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
        return n | mask;
    }

    static void Main() {
        int n = 17, l = 2, r = 3;
        Console.WriteLine(setAllRangeBits(n, l, r));
    }
}
JavaScript
function setAllRangeBits(n, l, r) {
    let mask = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
    return n | mask;
}

// Driver Code
let n = 17, l = 2, r = 3;
console.log(setAllRangeBits(n, l, r));

Output
23
Comment