• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
October 25, 2023 |440 Views
SDE Sheet - Toggle bits given range
  Share  2 Likes
Description
Discussion

This video is part of the Bit Manipulation section under the GFG SDE Sheet.

In this problem, we are 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 :

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

Try it out before watching the implementation of the problem in the video. We recommend watching the video, even if you can solve the problem. You may discover something new. All the best!!!

Do check out:-

Article: https://www.geeksforgeeks.org/toggle-bits-given-range/
Problem: https://practice.geeksforgeeks.org/problems/toggle-bits-given-range0952/1
SDE Sheet Link: https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/

Read More