-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathUnset.cpp
54 lines (40 loc) · 860 Bytes
/
Unset.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Name: Mehul Chaturvedi
IIT-Guwahati
*/
/*
PROBLEM STATEMENT
You are given two integers N and i. You need to make ith bit of binary representation of N to 0
and return the updated N.
Counting of bits start from 0 from right to left.
Input Format :
Two integers N and i (separated by space)
Output Format :
Updated N
Sample Input 1 :
7 2
Sample Output 1 :
3
Sample Input 2 :
12 1
Sample Output 2 :
12
*/
#include <bits/stdc++.h>
using namespace std;
int turnOffIthBit(int n, int i){
// k must be greater than 0
if (i < 0) return n;
// Do & of n with a number with all set bits except
// the k'th bit
return (n & ~(1 << (i)));
}
int main( int argc , char ** argv )
{
ios_base::sync_with_stdio(false) ;
cin.tie(NULL) ;
int n, i;
cin >> n >> i;
cout<< turnOffIthBit(n, i) <<endl;
return 0;
}