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;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )