2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 using System.Diagnostics.Contracts;
26 /// Base class for uint256 and uint160.
28 public class base_uint : IComparable<base_uint>, IEquatable<base_uint>
33 public double getDouble()
38 for (int i = 0; i < nWidth; i++)
47 public ulong GetLow64()
49 return pn[0] | (ulong)pn[1] << 32;
52 public uint GetLow32()
66 /// Zero or the position of highest non-zero bit plus one.
72 for (int pos = nWidth - 1; pos >= 0; pos--)
76 for (int bits = 31; bits > 0; bits--)
78 if ((pn[pos] & 1 << bits)!=0)
79 return 32 * pos + bits + 1;
88 public static bool operator !(base_uint a)
90 for (int i = 0; i < a.nWidth; i++)
101 public static bool operator <(base_uint a, base_uint b)
103 for (int i = a.nWidth - 1; i >= 0; i--)
105 if (a.pn[i] < b.pn[i])
109 else if (a.pn[i] > b.pn[i])
117 public static bool operator <=(base_uint a, base_uint b)
119 for (int i = a.nWidth - 1; i >= 0; i--)
121 if (a.pn[i] < b.pn[i])
125 else if (a.pn[i] > b.pn[i])
133 public static bool operator >(base_uint a, base_uint b)
135 for (int i = a.nWidth - 1; i >= 0; i--)
137 if (a.pn[i] > b.pn[i])
141 else if (a.pn[i] < b.pn[i])
149 public static bool operator >=(base_uint a, base_uint b)
151 for (int i = a.nWidth - 1; i >= 0; i--)
153 if (a.pn[i] > b.pn[i])
157 else if (a.pn[i] < b.pn[i])
165 public static bool operator ==(base_uint a, base_uint b)
167 if (object.ReferenceEquals(a, b))
172 for (int i = 0; i < a.nWidth; i++)
174 if (a.pn[i] != b.pn[i])
182 public static bool operator ==(base_uint a, ulong b)
184 if (a.pn[0] != (uint)b)
189 if (a.pn[1] != (uint)(b >> 32))
194 for (int i = 2; i < a.nWidth; i++)
204 public static bool operator !=(base_uint a, base_uint b)
209 public static bool operator !=(base_uint a, ulong b)
214 public static bool operator true(base_uint a)
219 public static bool operator false(base_uint a)
224 public static implicit operator byte[] (base_uint a)
226 return Interop.LEBytes(a.pn);
229 private static bool ArraysEqual(uint[] a, uint[] b)
231 Contract.Requires<ArgumentException>(a.Length == b.Length, "Array length mismatch.");
233 for (int i = 0; i < a.Length; i++)
243 public override int GetHashCode()
248 foreach (var element in pn)
250 hash = hash * 31 + element.GetHashCode();
256 public int CompareTo(base_uint item)
262 else if (this < item)
270 public bool Equals(base_uint a)
277 return ArraysEqual(pn, a.pn);
280 public override bool Equals(object o)
282 return Equals(o as base_uint);
285 public override string ToString()
287 return Interop.ToHex(Interop.ReverseBytes(this));