P17983 [GeeksforGeeks] Toggle bits given range0952
普及-
题目描述

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). After toggling all
the bits from 2nd to 3rd position we get
(10111){2} = (23)
Example 2:
Input:
N = 50 , L = 2 , R = 5
Output:
44
Explanation:
(50){10} = (110010). After toggling all
the bits from 2nd to 5th position we get
(101100){2} = (44)

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 <= 10^{5}
1 <= L<=R <=^{ }Number of Bits in N

输入格式

见题面

输出格式

见题面

样例输入
N = 17 , L = 2 , R = 3
样例输出
23