forked from shivam-0510-zz/Cpp-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitManipulation.cpp
More file actions
36 lines (35 loc) · 751 Bytes
/
BitManipulation.cpp
File metadata and controls
36 lines (35 loc) · 751 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
#include<iostream>
using namespace std;
int GetBit(int n,int pos)
{
int mask = 1<<pos;
return ((n&mask)!=0);
}
int SetBit(int n,int pos)
{
int mask = 1<<pos;
return (mask|n);
}
int ClearBit(int n,int pos)
{
int mask = 1<<pos;
return (n&(~mask));
}
int UpdateBit(int n,int pos,int value)
{
int mask = ~(1<<pos);
n=n&mask;
return (n|(value<<pos));
}
int main()
{
//To get bit at position 2 in the number 5
cout<<GetBit(5,2)<<endl;
//to set bit as 1 at the position 1
cout<<SetBit(5,1)<<endl;
//to clear bit or setting bit as 0 at the position 2
cout<<ClearBit(5,2)<<endl;
//to update the bit with the following value at the given position
cout<<UpdateBit(5,1,1)<<endl;
return 0;
}