-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop_bitwise.cpp
58 lines (42 loc) · 1.07 KB
/
op_bitwise.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
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;
bool isSet(int num, int bitPos)
{
return (num & (1 << bitPos)) != 0;
}
int setBit(int num, int bitPos)
{
return num | (1 << bitPos);
}
int main()
{
int n,m;
unsigned int un;
n = 5;
m = 9;
un = 5;
printf("~%d = %d\n",n,~n);
printf("~%u = %u\n",un,~un);
// O(1)
printf("%d and %d = %d\n",n,m,n&m);
printf("%d or %d = %d\n",n,m,n|m);
printf("%d xor %d = %d\n",n,m,n^m);
/* Left shift x << d
* x << d = x * 2^d */
printf("(1 << 1) = %d\n",1<<1);
printf("(1 << 2) = %d\n",1<<2);
printf("(1 << 3) = %d\n",1<<3);
printf("(1 << 4) = %d\n",1<<4);
printf("(2 << 3) = %d\n",2<<4);
/* Right shift x >> d
* x >> d = floor(x/2^d) */
printf("(5 >> 1) = %d\n",5>>1);
/* para variaveis long long*/
printf("(5 << 2) = %lld\n",5LL<<1);
if(isSet(9, 1))
printf("Sim\n");
else
printf("Nao\n");
printf("setbit no num %d na pos %d = %d\n",10,2,setBit(10,2));
return 0;
}