-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bitwise_Operations.cpp
100 lines (82 loc) · 2.02 KB
/
Bitwise_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <bits/stdc++.h>
using namespace std;
// Check if x is odd or even
bool is_odd(int x) {
return x & 1;
}
// Check if kth bit of x is set
bool check_kth_bit(int x, int k) {
return x >> k & 1;
}
// Return the count of set bits
int count_set_bits(int x) {
int res = 0;
for (int k = 0; k < 32; k++) {
res += x >> k & 1;
}
return res;
}
// Set the kth bit of x
int set_kth_bit(int x, int k) {
return x | (1 << k);
}
// Unset the kth bit of x
int unset_kth_bit(int x, int k) {
return x & (~(1 << k));
}
// Toggle the kth bit of x
int toggle_kth_bit(int x, int k) {
return x ^ (1 << k);
}
// Check if x is a power of 2
bool check_power_of_2(int x) {
return count_set_bits(x) == 1;
}
// Return the lowest bit of x
int lowest_bit(int x) {
return x & -x;
}
// Unset the lowest bit of x
int unset_lowest_bit(int x) {
return x & (x - 1);
}
// Print the binary representation of x
void to_binary(int x) {
for (int k = 31; k >= 0; k--) {
cout << (x >> k & 1);
}
cout << '\n';
}
void solve() {
int n;
cin >> n;
to_binary(n);
// Builtin function in GCC
// __builtin_popcountll, __builtin_ffsll, __builtin_clzll, __builtin_ctzll
// Count set bits
cout << __builtin_popcount(n) << '\n';
// Index of the lowest set bit
cout << __builtin_ffs(n) << '\n';
// Count of leading zeros
cout << __builtin_clz(n) << '\n';
// Count of trailing zeros
cout << __builtin_ctz(n) << '\n';
// Count set bits
cout << __popcount(n) << '\n';
// Return lenght of number
cout << __bit_width(n) << '\n';;
// Round up/down to the next power of two
cout << __bit_floor(n) << ' ' << __bit_ceil(n) << '\n';
// Count the leading/trailing zeros/ones
cout << __countl_zero(n) << ' ' << __countr_zero(n) << ' ' << __countl_one(n) << ' ' << __countr_one(n) << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}