-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeforces-bit.hpp
More file actions
54 lines (42 loc) · 1.08 KB
/
codeforces-bit.hpp
File metadata and controls
54 lines (42 loc) · 1.08 KB
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
#ifndef __CODEFORCES_BIT_HPP
#define __CODEFORCES_BIT_HPP
#include <version>
// workaround for polygon which uses c++ <= 17
#if __cpp_lib_bitops >= 201907L
#include <bit>
#else
#include <cstdint>
#include <limits>
#include <type_traits>
namespace std {
template <typename T>
constexpr std::enable_if_t<std::is_unsigned_v<T>, T> rotl(T x, int s);
template <typename T>
constexpr std::enable_if_t<std::is_unsigned_v<T>, T> rotr(T x, int s);
template <typename T>
constexpr std::enable_if_t<std::is_unsigned_v<T>, T> rotl(T x, int s) {
constexpr int N = std::numeric_limits<T>::digits;
int r = s % N;
if (r < 0) {
return rotr(x, -s);
}
if (r == 0) {
return x;
}
return (x << r) | (x >> (N - r));
}
template <typename T>
constexpr std::enable_if_t<std::is_unsigned_v<T>, T> rotr(T x, int s) {
constexpr int N = std::numeric_limits<T>::digits;
int r = s % N;
if (r < 0) {
return rotl(x, -s);
}
if (r == 0) {
return x;
}
return (x >> r) | (x << (N - r));
}
}; // namespace std
#endif
#endif // __CODEFORCES_BIT_HPP