-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcellnum.rb
More file actions
141 lines (110 loc) · 2.11 KB
/
cellnum.rb
File metadata and controls
141 lines (110 loc) · 2.11 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class Cellnum
attr_accessor :owner
attr_reader :code
@@Size=48
def initialize(x)
if x.is_a? Cellnum
@val=x.val
@code=x.code
else
@code=nil
@val=Cellnum.to_bin_range(x.to_i,@@Size);
end
end
def val
@val
end
def val=(v)
@val=v
@code=nil
end
def +(x)
Cellnum.new(@val+x.to_i)
end
def -(x)
Cellnum.new(@val-x.to_i)
end
def *(x)
Cellnum.new(@val*x.to_i)
end
def /(x)
Cellnum.new(@val/x.to_i)
end
def %(x)
Cellnum.new(@val % x.to_i)
end
def &(x)
Cellnum.new(@val & x.to_i)
end
def |(x)
Cellnum.new(@val | x.to_i)
end
def ^(x)
Cellnum.new(@val ^ x.to_i)
end
def not
Cellnum.new( to_bin.tr("01","10").rjust(@@Size,"1").to_i(2))
end
def ==(x)
@val==x.to_i
end
def <=(x)
@val<=x.to_i
end
def >=(x)
@val>=x.to_i
end
def >(x)
@val>x.to_i
end
def <(x)
@val<x.to_i
end
def to_i
return @val
end
def to_s
return "#"+@val.to_s
end
def to_bin
#Cellnum.to_bin(@val,@@Size)
Cellnum.to_bin_48(@val)
end
def Cellnum.to_bin_48(x)
y=to_bin_range_48(x);
(y>=0)? format("%b",x).rjust(48,"0") : format("%b",x)[3..-1].rjust(48,"1");
end
def Cellnum.to_bin_range_48(x);
x+=(140737488355328);
x%=281474976710656;
x-(140737488355328);
end
def Cellnum.to_bin(x,bits)
y=to_bin_range(x,bits);
(y>=0)? format("%b",x).rjust(bits,"0") : format("%b",x)[3..-1].rjust(bits,"1");
end
def Cellnum.to_bin_range(x,bits);
p=2**bits;
x+=(p>>1);
x%=p;
x-(p>>1);
end
def Cellnum.from_bin(x)
if x[0..0]=="1" then
return -(x.tr("01","10").to_i(2))-1
else
return x.to_i(2)
end
end
def Cellnum.to_16_bit(x)
to_bin(x,16)
end
def Cellnum.from_16_bit(x)
from_bin(x)
end
def to_code()
return @code if @code
@code=Disassembler.bin_to_code(to_bin)
return @code
end
end