Replace the Bit
PROBLEM :
Given two numbers n and k, change the kth bit of the number n to '0' if it is '1', else return the number n itself.
Input:
First line of the input contains an intger T denoting the number of test case. Each test contains a single line containg two space seperated integers n and k respectively.
Output:
For each test case output a single integer.
Constraints:
1<=T<=100
1<=N<=106
Example:
Input:
2
13 3
13 2
Ouput:
13
9
Explanation:
Test Case 1: n = 13 ('1101') k=3
return 13
Test Case 2: n = 13('1101') k=2
return 9('1001')
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
int t ;
cin>>t ;
while(t--){
int no,k ;
cin>>no>>k ;
int temp,count ;
count=0 ;
temp=no ;
while(temp){
temp=temp/2 ;
count++ ;
}
int p=count-k ;
cout<<(no&~(1<<p))<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Given two numbers n and k, change the kth bit of the number n to '0' if it is '1', else return the number n itself.
Input:
First line of the input contains an intger T denoting the number of test case. Each test contains a single line containg two space seperated integers n and k respectively.
Output:
For each test case output a single integer.
Constraints:
1<=T<=100
1<=N<=106
Example:
Input:
2
13 3
13 2
Ouput:
13
9
Explanation:
Test Case 1: n = 13 ('1101') k=3
return 13
Test Case 2: n = 13('1101') k=2
return 9('1001')
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
int t ;
cin>>t ;
while(t--){
int no,k ;
cin>>no>>k ;
int temp,count ;
count=0 ;
temp=no ;
while(temp){
temp=temp/2 ;
count++ ;
}
int p=count-k ;
cout<<(no&~(1<<p))<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Comments
Post a Comment