-
Notifications
You must be signed in to change notification settings - Fork 0
/
wnaf.cc
70 lines (49 loc) · 970 Bytes
/
wnaf.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
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <pthread.h>
#include <vector>
using namespace std;
int to_digit(vector<int> naf) {
int ret = 0;
int base = 1;
for (auto &b: naf) {
ret += base * b;
base <<= 1;
}
return ret;
}
int smod(int d, int mod) {
int modd = mod >> 1;
d %= mod;
return d > modd ? d - mod : d;
}
vector<int> to_naf(int d) {
vector<int> ret;
int w = 4;
int mod = 1 << w;
int i = 0;
while (d > 0) {
if (d & 1) {
ret.emplace_back(smod(d, mod));
d -= ret[i];
}
else ret.emplace_back(0);
d >>= 1;
++i;
}
// auto mod2 = mod >> 1;
// for (auto &each: ret)
// if (each >= mod2) each -= mod;
return ret;
}
int main() {
vector<int> vd;
int d = 100;
for (int d = 0; d < 1000; ++d) {
vd = to_naf(d);
cout << d << endl;
for (int i = vd.size() - 1; i >= 0; --i)
cout << vd[i] << ' ';
cout << endl;
// cout << to_digit(vd) << endl;
}
}