-
Notifications
You must be signed in to change notification settings - Fork 3
/
Basic Bit Manipulation.cpp
52 lines (39 loc) · 1.14 KB
/
Basic Bit Manipulation.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
#include <bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define U unsigned
string dectobin (U n)
{
string str;
while (n)
{
if (n & 1)
str += "1";
else
str += "0";
n >>= 1;
}
reverse (str.begin(),str.end());
return str;
}
int main ()
{
U n,t,x;
while (sf ("%u %u",&n,&x) != EOF)
{
pf ("Binary Form of the number : %s.\n",dectobin(n).c_str());
///Setting a bit
t = n | (1ULL << x);
pf ("\nAfter Setting Bit no. %u,\nChanged Number : %u, Changed Binary : %s.\n",x,t,dectobin(t).c_str());
///Clearing/Reseting a bit
t = n & ~(1ULL << x);
pf ("\nAfter Clearing Bit no. %u,\nChanged Number : %u, Changed Binary : %s.\n",x,t,dectobin(t).c_str());
///Toggling a bit
t = n ^ (1ULL << x);
pf ("\nAfter Toggling Bit no. %u,\nChanged Number : %u, Changed Binary : %s.\n",x,t,dectobin(t).c_str());
///Checking a bit
pf ("\nAfter Checking, bit no %u is %u.\n\n",x,(bool)(n & (1ULL << x)));
}
return 0;
}