-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.cpp
57 lines (53 loc) · 1.13 KB
/
common.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
//
// Created by nclab on 1/4/19.
//
#include "common.h"
int min(int a, int b)
{
return a<b?a:b;
}
void mp2bits(const mpz_t z, std::vector<bool>& bits)
{
if (!mpz_cmp_ui(z, 0))
{
bits.resize(1);
bits[0] = 0;
return;
}
unsigned long t;
mp_size_t n = mpz_size(z);
const mp_limb_t* zp = mpz_limbs_read(z); // a limb with 64 bits
//gmp_printf("%d %Zd\n", n, z);
bits.resize(n*64);
for (int i = 0; i<n-1; i++)
{
t = zp[i];
for (int j = 0; j < 64; j++)
{
bits[i*64+j] = (t&1);
t >>= 1;
}
}
t = zp[n-1];
int count = 0;
while (t > 0)
{
bits[(n-1)*64+count] = (t&1);
t>>=1;
count++;
}
bits.resize((n-1)*64+count);
}
void mp2bitString(const mpz_t z, std::string& bitString)
{
bitString.clear();
std::vector<bool> tmp;
//gmp_printf("!!!%Zd\n", z);
mp2bits(z, tmp);
int len = tmp.size();
char s[1] = "";
for (int i = 0; i<len;i++) {
s[0] = (char)((int)tmp[i]+(int)'0');
bitString += std::string(s); // 2^n bit store in bisString[n]
}
}