-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbit.cpp
More file actions
43 lines (32 loc) · 655 Bytes
/
bit.cpp
File metadata and controls
43 lines (32 loc) · 655 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
#include <bits/stdc++.h>
using namespace std;
bool getBit(int num, int i)
{
return ((num & (1 << i)) != 0);
}
int setBit(int num, int i)
{
return num | (1 << i);
}
int clearBit(int num, int i)
{
int mask = ~(1 << i);
// Return the update value
return num & mask;
}
int main()
{
int N = 70;
cout << "The bit at the 3rd position from LSB is: "
<< (getBit(N, 3) ? '1' : '0')
<< endl;
cout << "The value of the given number "
<< "after setting the bit at "
<< "LSB is: "
<< setBit(N, 0) << endl;
cout << "The value of the given number "
<< "after clearing the bit at "
<< "LSB is: "
<< clearBit(N, 0) << endl;
return 0;
}