-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_ith_bit.cpp
More file actions
58 lines (43 loc) · 960 Bytes
/
get_ith_bit.cpp
File metadata and controls
58 lines (43 loc) · 960 Bytes
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
55
56
57
58
#include<iostream>
using namespace std;
//finding the ith bit of the number acc to its position from right to left
int getithbit(int n,int i){
int mask = 1<<i;
return (n & mask) > 0 ? 1 : 0;
}
void setithbit(int &n , int i){
int mask = 1<<i;
n = (mask|n);
}
void clearithbit(int &n, int i){
int mask = ~ (1<<i);
n = n & mask;
}
void updateithbit(int &n,int i,int v){
clearithbit(n,i);
int mask = (v<<i);
n = n|mask;
}
void clearlastIbit(int &n,int i){
int mask = (-1<<i);
n = n & mask;
}
void clearBitsInRange(int n,int i,int j){
int a = (~0)<<(j+1);
int b = (1<<i) - 1;
int mask = a|b;
n = n & mask;
cout<<n;
}
int main(){
// int n,i;
// cin>> n;
// cout<<"enter the position of the bit "<<endl;
// cin>>i;
// cout << getithbit(n,i);
//setithbit(n,i);
//clearithbit(n,i);
//updateithbit(n,i,1);
// clearlastIbit(n,i);
clearBitsInRange(31,1,3);
}