1 | unit UWideInteger;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | type
|
---|
6 | IWideInteger = interface
|
---|
7 | function GetWidth: Integer;
|
---|
8 | procedure SetWidth(const Value: Integer);
|
---|
9 | function GetAsInteger: Integer;
|
---|
10 | procedure SetAsInteger(const Value: Integer);
|
---|
11 | function GetAsString: string;
|
---|
12 | procedure SetAsString(const Value: string);
|
---|
13 | function GetSign: Boolean;
|
---|
14 | procedure SetSign(const Value: Boolean);
|
---|
15 |
|
---|
16 | procedure Assign(Source: IWideInteger);
|
---|
17 | procedure Increment;
|
---|
18 | procedure Decrement;
|
---|
19 | procedure Add(Value: IWideInteger);
|
---|
20 | procedure Subtract(Value: IWideInteger);
|
---|
21 | property Width: Integer read GetWidth write SetWidth;
|
---|
22 | property AsInteger: Integer read GetAsInteger write SetAsInteger;
|
---|
23 | property AsString: string read GetAsString write SetAsString;
|
---|
24 | property Sign: Boolean read GetSign write SetSign;
|
---|
25 | end;
|
---|
26 |
|
---|
27 | TWideInteger = class(TInterfacedObject, IWideInteger)
|
---|
28 | private
|
---|
29 | Data: array of Byte;
|
---|
30 | FSign: Boolean;
|
---|
31 | function GetWidth: Integer;
|
---|
32 | procedure SetWidth(const Value: Integer);
|
---|
33 | function GetAsInteger: Integer;
|
---|
34 | procedure SetAsInteger(const Value: Integer);
|
---|
35 | function GetAsString: string;
|
---|
36 | procedure SetAsString(const Value: string);
|
---|
37 | function GetSign: Boolean;
|
---|
38 | procedure SetSign(const Value: Boolean);
|
---|
39 | public
|
---|
40 | procedure Assign(Source: IWideInteger);
|
---|
41 | procedure Increment;
|
---|
42 | procedure Decrement;
|
---|
43 | procedure Add(Value: IWideInteger);
|
---|
44 | procedure Subtract(Value: IWideInteger);
|
---|
45 | property Width: Integer read GetWidth write SetWidth;
|
---|
46 | property AsInteger: Integer read GetAsInteger write SetAsInteger;
|
---|
47 | property AsString: string read GetAsString write SetAsString;
|
---|
48 | property Sign: Boolean read GetSign write SetSign;
|
---|
49 | end;
|
---|
50 |
|
---|
51 | implementation
|
---|
52 |
|
---|
53 | { TWideInteger }
|
---|
54 |
|
---|
55 | procedure TWideInteger.Add(Value: IWideInteger);
|
---|
56 | var
|
---|
57 | I: Integer;
|
---|
58 | Carry: Integer;
|
---|
59 | begin
|
---|
60 | Carry := 0;
|
---|
61 | for I := 0 to Width - 1 do begin
|
---|
62 | Carry := Carry + Value.Data[I];
|
---|
63 | Data[I] := Carry;
|
---|
64 | Carry := Carry shr 8;
|
---|
65 | end;
|
---|
66 | end;
|
---|
67 |
|
---|
68 | procedure TWideInteger.Assign(Source: TWideInteger);
|
---|
69 | var
|
---|
70 | I: Integer;
|
---|
71 | begin
|
---|
72 | Width := Source.Width;
|
---|
73 | for I := 0 to Width - 1 do
|
---|
74 | Data[I] := Source.Data[I];
|
---|
75 | end;
|
---|
76 |
|
---|
77 | procedure TWideInteger.Decrement;
|
---|
78 | begin
|
---|
79 | Subtract(1);
|
---|
80 | end;
|
---|
81 |
|
---|
82 | function TWideInteger.GetAsInteger: Integer;
|
---|
83 | begin
|
---|
84 | Result := Data[0] or (Data[1] shl 8) or (Data[2] shl 16) or (Data[3] shl 24)
|
---|
85 | or (Sign shl 31);
|
---|
86 | end;
|
---|
87 |
|
---|
88 | function TWideInteger.GetAsString: string;
|
---|
89 | begin
|
---|
90 | Result := '';
|
---|
91 | end;
|
---|
92 |
|
---|
93 | function TWideInteger.GetSign: Boolean;
|
---|
94 | begin
|
---|
95 | Result := FSign;
|
---|
96 | end;
|
---|
97 |
|
---|
98 | function TWideInteger.GetWidth: Integer;
|
---|
99 | begin
|
---|
100 | Result := Length(Data);
|
---|
101 | end;
|
---|
102 |
|
---|
103 | procedure TWideInteger.Increment;
|
---|
104 | begin
|
---|
105 | Add(1);
|
---|
106 | end;
|
---|
107 |
|
---|
108 | procedure TWideInteger.SetAsInteger(const Value: Integer);
|
---|
109 | begin
|
---|
110 | Width := 4;
|
---|
111 | Data[0] := Value and $ff;
|
---|
112 | Data[1] := (Value shr 8) and $ff;
|
---|
113 | Data[2] := (Value shr 16) and $ff;
|
---|
114 | Data[3] := (Value shr 24) and $3f;
|
---|
115 | end;
|
---|
116 |
|
---|
117 | procedure TWideInteger.SetAsString(const Value: string);
|
---|
118 | begin
|
---|
119 |
|
---|
120 | end;
|
---|
121 |
|
---|
122 | procedure TWideInteger.SetSign(const Value: Boolean);
|
---|
123 | begin
|
---|
124 | FSign := Value;
|
---|
125 | end;
|
---|
126 |
|
---|
127 | procedure TWideInteger.SetWidth(const Value: Integer);
|
---|
128 | begin
|
---|
129 | SetLength(Data, Value);
|
---|
130 | end;
|
---|
131 |
|
---|
132 | procedure TWideInteger.Subtract(Value: TWideInteger);
|
---|
133 | begin
|
---|
134 |
|
---|
135 | Add(Value);
|
---|
136 | end;
|
---|
137 |
|
---|
138 | end.
|
---|