forked from VinayBelwal/Hactober-22-Programs-and-Projects-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bits.cpp
49 lines (48 loc) · 805 Bytes
/
bits.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
#include"bits/stdc++.h"
using namespace std;
int getbit(int n,int pos)
{
return((n&(1<<pos))!=0);
}
int setbit(int n,int pos)
{
return((n|(1<<pos)));
}
int clearbit(int n,int pos)
{
int mask=~(1<<pos);
return(n & mask);
}
int updatebit(int n, int pos)
{
int mask=~(1<<pos);
n=n&mask;
return((n|(0<<pos)));
}
int countone(int n)
{
int count=0;
while(n!=0)
{
n=n & n-1;
count++;
}
return count;
}
bool ispowerof2(int n)
{
return(n && !(n & n-1));
}
int main()
{
int n,pos;
cin>>n;
cin>>pos;
cout<<getbit(n,pos)<<endl;
cout<<setbit(n,pos)<<endl;
cout<<clearbit(n,pos)<<endl;
cout<<updatebit(n,pos);
cout<<ispowerof2(32)<<endl;
cout<<countone(16);
return 0;
}