Toggle bits given range | Practice | GeeksforGeeks

Given a non-negative number N and two values L and R. The problem is to toggle the bits in the range L to R in the binary representation of N, i.e, to toggle bits from the rightmost Lth bit to the rightmost Rth bit. A toggle operation flips a bit 0 to 1 and a bit 1 to 0. Print N after the bits are toggled.
 
Example 1:
Input:
N = 17 , L = 2 , R = 3
Output:
23
Explanation:
(17)10 = (10001)2.  After toggling all
the bits from 2nd to 3rd position we get
(10111)2 = (23)10
Example 2:
Input:
N = 50 , L = 2 , R = 5
Output:
44
Explanation:
(50)10 = (110010)2.  After toggling all
the bits from 2nd to 3rd position we get
(101100)2 = (44)10
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function toggleBits() which takes 3 Integers N,L and R as input and returns the answer.
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 105
1 <= L<=R <= Number of Bits in N
+1
pinakshkumar5 days ago
JAVA O(1) SOLUTION :
 
class Solution {
    static int toggleBits(int N , int L , int R) {
  
   int m1 = 1;
   
   int m2 = m1 <<(R - L +1);
    
   int m3 = m2 -1 ;
   
   int m4 = m3 << (L -1);
   
   return m4^N ;
   
    }
}
0
mrnecto1 week ago
class Solution {
 public:
   int toggleBits(int N , int L , int R) {
       // code here
       while(L++ <= R)
           N ^= 1 << L - 2;
       return N;
   }
};
0
mrnecto
This comment was deleted.
0
madhukartemba2 weeks ago
JAVA SOLUTION:
class Solution {
    static int toggleBits(int N , int L , int R) {
        int ans = 0;
        
        for(int i=0; i<31; i++)
        {
            int bit = (N>>i)&1;
            //System.out.println(bit);
            if(i+1<=R && i+1>=L)
            {
                bit = 1-bit;
            }
            
            ans = ans | (bit<<i);
            
        }
        
        return ans;
        
    }
};
0
antonyxaviercross2 months ago
    int toggleBits(int N , int L , int R) {              int noOfBit = R - L + 1;       N ^= ((0xFFFFFFFF >> 32 - noOfBit) << L-1);  // Toggle the bits from L to R;            return N;   }
0
swarnendumondal20084 months ago
C++ Easy Solution:
int toggleBits(int n , int l , int r)
    {
        for(int i=l;i<=r;i++)
        {
            n = n^(1<<(i-1));
        }
        return n;
    }
+1
aniketsingh27220016 months ago
int toggleBits(int N , int L , int R) {       // code here        int mask=1<<(R-L+1);    mask=mask-1;    mask=mask<<(L-1);    N=N^mask;    return N;   }
0
vishalvatsal306 months ago
    int toggleBits(int N , int L , int R) {       // code here       int x = 1;       if(L != 0)           x = x << (L-1);       int i = 0;       while(i <= (R-L)){           N^=x;           //cout << N << endl;           x = x << 1;           i++;       }       return N;   }
0
ashishpawar2803016 months ago
class Solution {   static int toggleBits(int N , int L , int R) {       // code here       int start = (int)Math.pow(2, L-1);       int end   = (int)Math.pow(2, R-1);              while(start<=end){           if((N&start)==start){               N = N-start;           }else{               N = N+start;           }           start = start<<1;       }              return N;   }};
0
Himanshu Dubey9 months ago
Himanshu Dubey
int toggleBits(int N , int L , int R)    {     int n = pow(2,R-L+1) - 1;     int x = n<<(L-1);     int res = x^N;     return res;    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.