Created
January 3, 2020 13:51
-
-
Save SuryaPratapK/312c4736b6a1d39686b9f00bbd7f9d34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
int main() { | |
//code | |
int tc; | |
cin>>tc; | |
while(tc--) | |
{ | |
int n,l,r; | |
cin>>n>>l>>r; | |
int val = 0; | |
int power = 0; | |
int mask = 1; | |
int i=1; | |
//Include all rightmost bits as it is | |
while(i<l) | |
{ | |
if(mask&n) | |
val += pow(2,power); | |
mask<<=1; | |
power+=1; | |
++i; | |
} | |
//Toggle bits in range and include | |
while(i<=r) | |
{ | |
if(!(mask&n)) | |
val += pow(2,power); | |
mask<<=1; | |
power+=1; | |
++i; | |
} | |
//Include all leftmost bits as it is | |
while(i<=10) | |
{ | |
if(mask&n) | |
val += pow(2,power); | |
mask<<=1; | |
power+=1; | |
++i; | |
} | |
cout<<val<<"\n"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment