-
Notifications
You must be signed in to change notification settings - Fork 0
/
bineq.cc
59 lines (46 loc) · 1.05 KB
/
bineq.cc
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
#include <iostream>
#include <iomanip>
#include <limits>
#include <cassert>
bool get_bit(unsigned num, unsigned bit)
{
return (num >> bit) & 0x01;
}
unsigned get_range(unsigned num, unsigned start, unsigned len)
{
size_t const digits = std::numeric_limits<unsigned>::digits;
std::cout << digits << std::endl;
if (start + len > digits) {
return 0;
}
unsigned const shift = (digits - (start + len));
unsigned const mask = 1 << (len);
num = num >> shift;
num = num & ~mask;
return num;
}
int is_bineq(unsigned num)
{
size_t const digits = std::numeric_limits<unsigned>::digits;
for (unsigned len = 0; len < digits / 2; ++len) {
unsigned start = 0;
bool failed = false;
while (start + 2*len < digits) {
failed = (get_range(num, start, len) != get_range(num, start + len, len));
if (failed) {
break;
}
start += len;
}
if (!failed) {
return len;
}
}
return -1;
}
int main()
{
std::cout << std::hex << get_range(0xFF000000, 0, 8) << std::dec << std::endl;
assert(get_range(0xFFFFFFFF, 0, 8) == 0xFF);
return 0;
}