Given a non-negative integer n and two integers l and r, toggle all bits in the binary representation of n from the lth to the rth least significant bit (inclusive). The least significant bit is numbered 1.
A toggle operation changes a bit from 0 to 1 or from 1 to 0.
Examples:
Input: n = 17, l = 1, r = 3
Output: 22
Explanation: (17)10 = (10001)2. After toggling the bits from the 1st to the 3rd position, the binary representation becomes (10110)2, which is 22 in decimal.Input: n = 50, l = 2, r = 5
Output: 44
Explanation: (50)10 = (110010)2. After toggling the bits from the 2nd to the 5th position, the binary representation becomes (101100)2, which is 44 in decimal.
Table of Content
Iterate Through the Range - O(r - l + 1) Time and O(1) Space
The idea is to traverse each bit position from l to r and toggle it using XOR. For each position, create a mask with only that bit set and XOR it with n.
- Traverse the bit positions from l to r.
- Create a mask for the current bit using 1 << (i - 1).
- XOR n with the mask to toggle the bit.
- Return the updated value.
#include <iostream>
using namespace std;
int toggleBits(int n, int l, int r)
{
for (int i = l; i <= r; i++) {
int mask = 1 << (i - 1);
// Toggle the ith bit
if (n & mask)
n ^= mask;
else
n |= mask;
}
return n;
}
int main()
{
int n = 50;
int l = 2;
int r = 5;
cout << toggleBits(n, l, r);
return 0;
}
class GFG {
static int toggleBits(int n, int l, int r) {
for (int i = l; i <= r; i++) {
int mask = 1 << (i - 1);
// Toggle the ith bit
if ((n & mask) != 0)
// Set bit -> unset
n ^= mask;
else
// Unset bit -> set
n |= mask;
}
return n;
}
public static void main(String[] args) {
int n = 50;
int l = 2;
int r = 5;
System.out.println(toggleBits(n, l, r));
}
}
def toggleBits(n, l, r):
for i in range(l, r + 1):
mask = 1 << (i - 1)
# Toggle the ith bit
if n & mask:
# Set bit -> unset
n ^= mask
else:
# Unset bit -> set
n |= mask
return n
if __name__ == "__main__":
n = 50
l = 2
r = 5
print(toggleBits(n, l, r))
using System;
class GFG
{
static int toggleBits(int n, int l, int r)
{
for (int i = l; i <= r; i++)
{
int mask = 1 << (i - 1);
// Toggle the ith bit
if ((n & mask) != 0)
// Set bit -> unset
n ^= mask;
else
// Unset bit -> set
n |= mask;
}
return n;
}
public static void Main()
{
int n = 50;
int l = 2;
int r = 5;
Console.WriteLine(toggleBits(n, l, r));
}
}
function toggleBits(n, l, r) {
for (let i = l; i <= r; i++) {
const mask = 1 << (i - 1);
// Toggle the ith bit
if (n & mask)
// Set bit -> unset
n ^= mask;
else
// Unset bit -> set
n |= mask;
}
return n;
}
// Driver code
const n = 50;
const l = 2;
const r = 5;
console.log(toggleBits(n, l, r));
Output
44
Using Bitwise XOR - O(1) Time and O(1) Space
- Create a mask as = ((1 << r) - 1) ^ ((1 << (l-1)) - 1) or as ((1 <<r)-l). This produces a number having r number of bits and bits in the range l to r (from rightmost end in binary representation) are the only set bits.
- Now, perform n = n ^ mask. This toggles the bits in the range l to r in n..
#include <iostream>
using namespace std;
int toggleBits(int n, int l, int r) {
// Create a mask with bits from l to r set
int mask = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
// Toggle bits in the given range
return n ^ mask;
}
int main() {
int n = 17;
int l = 1;
int r = 3;
cout << toggleBits(n, l, r);
return 0;
}
class GFG {
static int toggleBits(int n, int l, int r) {
// Create a mask with bits from l to r set
int mask = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
// Toggle bits in the given range
return n ^ mask;
}
public static void main(String[] args) {
int n = 17;
int l = 1;
int r = 3;
System.out.println(toggleBits(n, l, r));
}
}
def toggleBits(n, l, r):
# Create a mask with bits from l to r set to 1
mask = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1)
# Toggle the bits in the range l to r
return n ^ mask
if __name__ == "__main__":
n = 17
l = 1
r = 3
print(toggleBits(n, l, r))
using System;
class GFG {
static int toggleBits(int n, int l, int r) {
// Create a mask with bits from l to r set
int mask = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
// Toggle bits in the given range
return n ^ mask;
}
public static void Main() {
int n = 17;
int l = 1;
int r = 3;
Console.WriteLine(toggleBits(n, l, r));
}
}
function toggleBits(n, l, r) {
// Create a mask with bits from l to r set to 1
const mask = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
// Toggle the bits in the range l to r
return n ^ mask;
}
// Driver code
const n = 17;
const l = 1;
const r = 3;
console.log(toggleBits(n, l, r));
Output
22