-
Notifications
You must be signed in to change notification settings - Fork 2
/
bit_operations.cpp
42 lines (31 loc) · 918 Bytes
/
bit_operations.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
/****************************************************************************
File name: bit_operations.cpp
Author: babajr
*****************************************************************************/
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
// int n = 10;
int n;
int res, res1, res2, res3;
int pos;
cout << "Enter the decimal number:" << endl;
cin >> n;
cout << "Enter the position: " << endl;
cin >> pos;
//setting bit
res = n | (1 << pos);
printf("Set bit:%d\n", res); // 1010 => 1110
//clearing bit 1010
res1 = n & ~(1 << pos);
printf("Result after clearing bit:%d\n", res1);//1110 => 1010
//checking bit
res2 = n & (1 << pos);
printf("Checking bit:%d\n", res2); //False
//toggling bit
res3 = n ^ (1 << pos);
printf("Result after toggling bit:%d\n", res3);//1110
return 0;
}