forked from eest9/trit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ternary.h
52 lines (49 loc) · 960 Bytes
/
ternary.h
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
#ifndef TERNARY_INCLUDED
#define TERNARY_INCLUDED
void var_to_tryte(short in, short *out);
void var_to_tryte(short in, short *out)
{
bool carry=0;
if(in<0)
{
in*=(-1);
for(char i=10;i>=0;i--)
{
out[i]=in%3+1+carry;
in/=3;
if(out[i]>2)
{
carry=1;
out[i]-=3;
}
else
{
carry=0;
}
out[i]--;
if(out[i]==-1)
out[i]=1;
else if(out[i]==1)
out[i]=-1;
}
}
else
{
for(char i=10;i>=0;i--)
{
out[i]=in%3+1+carry;
in/=3;
if(out[i]>2)
{
carry=1;
out[i]-=3;
}
else
{
carry=0;
}
out[i]--;
}
}
}
#endif // TERNARY_INCLUDED