]> Git Repo - VerusCoin.git/blame - uint256.h
Fix CRLF
[VerusCoin.git] / uint256.h
CommitLineData
8bd66202
GA
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Distributed under the MIT/X11 software license, see the accompanying
3// file license.txt or http://www.opensource.org/licenses/mit-license.php.
4
5#include <limits.h>
6#include <string>
7#if defined(_MSC_VER) || defined(__BORLANDC__)
8typedef __int64 int64;
9typedef unsigned __int64 uint64;
10#else
11typedef long long int64;
12typedef unsigned long long uint64;
13#endif
14#if defined(_MSC_VER) && _MSC_VER < 1300
15#define for if (false) ; else for
16#endif
17
18
19inline int Testuint256AdHoc(vector<string> vArg);
20
21
22
23// We have to keep a separate base class without constructors
24// so the compiler will let us use it in a union
25template<unsigned int BITS>
26class base_uint
27{
28protected:
29 enum { WIDTH=BITS/32 };
30 unsigned int pn[WIDTH];
31public:
32
33 bool operator!() const
34 {
35 for (int i = 0; i < WIDTH; i++)
36 if (pn[i] != 0)
37 return false;
38 return true;
39 }
40
41 const base_uint operator~() const
42 {
43 base_uint ret;
44 for (int i = 0; i < WIDTH; i++)
45 ret.pn[i] = ~pn[i];
46 return ret;
47 }
48
49 const base_uint operator-() const
50 {
51 base_uint ret;
52 for (int i = 0; i < WIDTH; i++)
53 ret.pn[i] = ~pn[i];
54 ret++;
55 return ret;
56 }
57
58
59 base_uint& operator=(uint64 b)
60 {
61 pn[0] = (unsigned int)b;
62 pn[1] = (unsigned int)(b >> 32);
63 for (int i = 2; i < WIDTH; i++)
64 pn[i] = 0;
65 return *this;
66 }
67
68 base_uint& operator^=(const base_uint& b)
69 {
70 for (int i = 0; i < WIDTH; i++)
71 pn[i] ^= b.pn[i];
72 return *this;
73 }
74
75 base_uint& operator&=(const base_uint& b)
76 {
77 for (int i = 0; i < WIDTH; i++)
78 pn[i] &= b.pn[i];
79 return *this;
80 }
81
82 base_uint& operator|=(const base_uint& b)
83 {
84 for (int i = 0; i < WIDTH; i++)
85 pn[i] |= b.pn[i];
86 return *this;
87 }
88
89 base_uint& operator^=(uint64 b)
90 {
91 pn[0] ^= (unsigned int)b;
92 pn[1] ^= (unsigned int)(b >> 32);
93 return *this;
94 }
95
96 base_uint& operator&=(uint64 b)
97 {
98 pn[0] &= (unsigned int)b;
99 pn[1] &= (unsigned int)(b >> 32);
100 return *this;
101 }
102
103 base_uint& operator|=(uint64 b)
104 {
105 pn[0] |= (unsigned int)b;
106 pn[1] |= (unsigned int)(b >> 32);
107 return *this;
108 }
109
110 base_uint& operator<<=(unsigned int shift)
111 {
112 base_uint a(*this);
113 for (int i = 0; i < WIDTH; i++)
114 pn[i] = 0;
115 int k = shift / 32;
116 shift = shift % 32;
117 for (int i = 0; i < WIDTH; i++)
118 {
119 if (i+k+1 < WIDTH && shift != 0)
120 pn[i+k+1] |= (a.pn[i] >> (32-shift));
121 if (i+k < WIDTH)
122 pn[i+k] |= (a.pn[i] << shift);
123 }
124 return *this;
125 }
126
127 base_uint& operator>>=(unsigned int shift)
128 {
129 base_uint a(*this);
130 for (int i = 0; i < WIDTH; i++)
131 pn[i] = 0;
132 int k = shift / 32;
133 shift = shift % 32;
134 for (int i = 0; i < WIDTH; i++)
135 {
136 if (i-k-1 >= 0 && shift != 0)
137 pn[i-k-1] |= (a.pn[i] << (32-shift));
138 if (i-k >= 0)
139 pn[i-k] |= (a.pn[i] >> shift);
140 }
141 return *this;
142 }
143
144 base_uint& operator+=(const base_uint& b)
145 {
146 uint64 carry = 0;
147 for (int i = 0; i < WIDTH; i++)
148 {
149 uint64 n = carry + pn[i] + b.pn[i];
150 pn[i] = n & 0xffffffff;
151 carry = n >> 32;
152 }
153 return *this;
154 }
155
156 base_uint& operator-=(const base_uint& b)
157 {
158 *this += -b;
159 return *this;
160 }
161
162 base_uint& operator+=(uint64 b64)
163 {
164 base_uint b;
165 b = b64;
166 *this += b;
167 return *this;
168 }
169
170 base_uint& operator-=(uint64 b64)
171 {
172 base_uint b;
173 b = b64;
174 *this += -b;
175 return *this;
176 }
177
178
179 base_uint& operator++()
180 {
181 // prefix operator
182 int i = 0;
183 while (++pn[i] == 0 && i < WIDTH-1)
184 i++;
185 return *this;
186 }
187
188 const base_uint operator++(int)
189 {
190 // postfix operator
191 const base_uint ret = *this;
192 ++(*this);
193 return ret;
194 }
195
196 base_uint& operator--()
197 {
198 // prefix operator
199 int i = 0;
200 while (--pn[i] == -1 && i < WIDTH-1)
201 i++;
202 return *this;
203 }
204
205 const base_uint operator--(int)
206 {
207 // postfix operator
208 const base_uint ret = *this;
209 --(*this);
210 return ret;
211 }
212
213
214 friend inline bool operator<(const base_uint& a, const base_uint& b)
215 {
216 for (int i = base_uint::WIDTH-1; i >= 0; i--)
217 {
218 if (a.pn[i] < b.pn[i])
219 return true;
220 else if (a.pn[i] > b.pn[i])
221 return false;
222 }
223 return false;
224 }
225
226 friend inline bool operator<=(const base_uint& a, const base_uint& b)
227 {
228 for (int i = base_uint::WIDTH-1; i >= 0; i--)
229 {
230 if (a.pn[i] < b.pn[i])
231 return true;
232 else if (a.pn[i] > b.pn[i])
233 return false;
234 }
235 return true;
236 }
237
238 friend inline bool operator>(const base_uint& a, const base_uint& b)
239 {
240 for (int i = base_uint::WIDTH-1; i >= 0; i--)
241 {
242 if (a.pn[i] > b.pn[i])
243 return true;
244 else if (a.pn[i] < b.pn[i])
245 return false;
246 }
247 return false;
248 }
249
250 friend inline bool operator>=(const base_uint& a, const base_uint& b)
251 {
252 for (int i = base_uint::WIDTH-1; i >= 0; i--)
253 {
254 if (a.pn[i] > b.pn[i])
255 return true;
256 else if (a.pn[i] < b.pn[i])
257 return false;
258 }
259 return true;
260 }
261
262 friend inline bool operator==(const base_uint& a, const base_uint& b)
263 {
264 for (int i = 0; i < base_uint::WIDTH; i++)
265 if (a.pn[i] != b.pn[i])
266 return false;
267 return true;
268 }
269
270 friend inline bool operator==(const base_uint& a, uint64 b)
271 {
272 if (a.pn[0] != (unsigned int)b)
273 return false;
274 if (a.pn[1] != (unsigned int)(b >> 32))
275 return false;
276 for (int i = 2; i < base_uint::WIDTH; i++)
277 if (a.pn[i] != 0)
278 return false;
279 return true;
280 }
281
282 friend inline bool operator!=(const base_uint& a, const base_uint& b)
283 {
284 return (!(a == b));
285 }
286
287 friend inline bool operator!=(const base_uint& a, uint64 b)
288 {
289 return (!(a == b));
290 }
291
292
293
294 std::string GetHex() const
295 {
296 char psz[sizeof(pn)*2 + 1];
297 for (int i = 0; i < sizeof(pn); i++)
298 sprintf(psz + i*2, "%02x", ((unsigned char*)pn)[sizeof(pn) - i - 1]);
299 return string(psz, psz + sizeof(pn)*2);
300 }
301
302 void SetHex(const char* psz)
303 {
304 for (int i = 0; i < WIDTH; i++)
305 pn[i] = 0;
306
307 // skip leading spaces
308 while (isspace(*psz))
309 psz++;
310
311 // skip 0x
312 if (psz[0] == '0' && tolower(psz[1]) == 'x')
313 psz += 2;
314
315 // hex string to uint
316 static char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 };
317 const char* pbegin = psz;
318 while (phexdigit[*psz] || *psz == '0')
319 psz++;
320 psz--;
321 unsigned char* p1 = (unsigned char*)pn;
322 unsigned char* pend = p1 + WIDTH * 4;
323 while (psz >= pbegin && p1 < pend)
324 {
325 *p1 = phexdigit[(unsigned char)*psz--];
326 if (psz >= pbegin)
327 {
328 *p1 |= (phexdigit[(unsigned char)*psz--] << 4);
329 p1++;
330 }
331 }
332 }
333
334 void SetHex(const std::string& str)
335 {
336 SetHex(str.c_str());
337 }
338
339 std::string ToString() const
340 {
341 return (GetHex());
342 }
343
344 unsigned char* begin()
345 {
346 return (unsigned char*)&pn[0];
347 }
348
349 unsigned char* end()
350 {
351 return (unsigned char*)&pn[WIDTH];
352 }
353
354 unsigned int size()
355 {
356 return sizeof(pn);
357 }
358
359
360 unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const
361 {
362 return sizeof(pn);
363 }
364
365 template<typename Stream>
366 void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const
367 {
368 s.write((char*)pn, sizeof(pn));
369 }
370
371 template<typename Stream>
372 void Unserialize(Stream& s, int nType=0, int nVersion=VERSION)
373 {
374 s.read((char*)pn, sizeof(pn));
375 }
376
377
378 friend class uint160;
379 friend class uint256;
380 friend inline int Testuint256AdHoc(vector<string> vArg);
381};
382
383typedef base_uint<160> base_uint160;
384typedef base_uint<256> base_uint256;
385
386
387
388//
389// uint160 and uint256 could be implemented as templates, but to keep
390// compile errors and debugging cleaner, they're copy and pasted.
391//
392
393
394
395//////////////////////////////////////////////////////////////////////////////
396//
397// uint160
398//
399
400class uint160 : public base_uint160
401{
402public:
403 typedef base_uint160 basetype;
404
405 uint160()
406 {
407 for (int i = 0; i < WIDTH; i++)
408 pn[i] = 0;
409 }
410
411 uint160(const basetype& b)
412 {
413 for (int i = 0; i < WIDTH; i++)
414 pn[i] = b.pn[i];
415 }
416
417 uint160& operator=(const basetype& b)
418 {
419 for (int i = 0; i < WIDTH; i++)
420 pn[i] = b.pn[i];
421 return *this;
422 }
423
424 uint160(uint64 b)
425 {
426 pn[0] = (unsigned int)b;
427 pn[1] = (unsigned int)(b >> 32);
428 for (int i = 2; i < WIDTH; i++)
429 pn[i] = 0;
430 }
431
432 uint160& operator=(uint64 b)
433 {
434 pn[0] = (unsigned int)b;
435 pn[1] = (unsigned int)(b >> 32);
436 for (int i = 2; i < WIDTH; i++)
437 pn[i] = 0;
438 return *this;
439 }
440
441 explicit uint160(const std::string& str)
442 {
443 SetHex(str);
444 }
445
446 explicit uint160(const std::vector<unsigned char>& vch)
447 {
448 if (vch.size() == sizeof(pn))
449 memcpy(pn, &vch[0], sizeof(pn));
450 else
451 *this = 0;
452 }
453};
454
455inline bool operator==(const uint160& a, uint64 b) { return (base_uint160)a == b; }
456inline bool operator!=(const uint160& a, uint64 b) { return (base_uint160)a != b; }
457inline const uint160 operator<<(const base_uint160& a, unsigned int shift) { return uint160(a) <<= shift; }
458inline const uint160 operator>>(const base_uint160& a, unsigned int shift) { return uint160(a) >>= shift; }
459inline const uint160 operator<<(const uint160& a, unsigned int shift) { return uint160(a) <<= shift; }
460inline const uint160 operator>>(const uint160& a, unsigned int shift) { return uint160(a) >>= shift; }
461
462inline const uint160 operator^(const base_uint160& a, const base_uint160& b) { return uint160(a) ^= b; }
463inline const uint160 operator&(const base_uint160& a, const base_uint160& b) { return uint160(a) &= b; }
464inline const uint160 operator|(const base_uint160& a, const base_uint160& b) { return uint160(a) |= b; }
465inline const uint160 operator+(const base_uint160& a, const base_uint160& b) { return uint160(a) += b; }
466inline const uint160 operator-(const base_uint160& a, const base_uint160& b) { return uint160(a) -= b; }
467
468inline bool operator<(const base_uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; }
469inline bool operator<=(const base_uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; }
470inline bool operator>(const base_uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; }
471inline bool operator>=(const base_uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; }
472inline bool operator==(const base_uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; }
473inline bool operator!=(const base_uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; }
474inline const uint160 operator^(const base_uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
475inline const uint160 operator&(const base_uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; }
476inline const uint160 operator|(const base_uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; }
477inline const uint160 operator+(const base_uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; }
478inline const uint160 operator-(const base_uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; }
479
480inline bool operator<(const uint160& a, const base_uint160& b) { return (base_uint160)a < (base_uint160)b; }
481inline bool operator<=(const uint160& a, const base_uint160& b) { return (base_uint160)a <= (base_uint160)b; }
482inline bool operator>(const uint160& a, const base_uint160& b) { return (base_uint160)a > (base_uint160)b; }
483inline bool operator>=(const uint160& a, const base_uint160& b) { return (base_uint160)a >= (base_uint160)b; }
484inline bool operator==(const uint160& a, const base_uint160& b) { return (base_uint160)a == (base_uint160)b; }
485inline bool operator!=(const uint160& a, const base_uint160& b) { return (base_uint160)a != (base_uint160)b; }
486inline const uint160 operator^(const uint160& a, const base_uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
487inline const uint160 operator&(const uint160& a, const base_uint160& b) { return (base_uint160)a & (base_uint160)b; }
488inline const uint160 operator|(const uint160& a, const base_uint160& b) { return (base_uint160)a | (base_uint160)b; }
489inline const uint160 operator+(const uint160& a, const base_uint160& b) { return (base_uint160)a + (base_uint160)b; }
490inline const uint160 operator-(const uint160& a, const base_uint160& b) { return (base_uint160)a - (base_uint160)b; }
491
492inline bool operator<(const uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; }
493inline bool operator<=(const uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; }
494inline bool operator>(const uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; }
495inline bool operator>=(const uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; }
496inline bool operator==(const uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; }
497inline bool operator!=(const uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; }
498inline const uint160 operator^(const uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
499inline const uint160 operator&(const uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; }
500inline const uint160 operator|(const uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; }
501inline const uint160 operator+(const uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; }
502inline const uint160 operator-(const uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; }
503
504
505
506
507
508
509//////////////////////////////////////////////////////////////////////////////
510//
511// uint256
512//
513
514class uint256 : public base_uint256
515{
516public:
517 typedef base_uint256 basetype;
518
519 uint256()
520 {
521 for (int i = 0; i < WIDTH; i++)
522 pn[i] = 0;
523 }
524
525 uint256(const basetype& b)
526 {
527 for (int i = 0; i < WIDTH; i++)
528 pn[i] = b.pn[i];
529 }
530
531 uint256& operator=(const basetype& b)
532 {
533 for (int i = 0; i < WIDTH; i++)
534 pn[i] = b.pn[i];
535 return *this;
536 }
537
538 uint256(uint64 b)
539 {
540 pn[0] = (unsigned int)b;
541 pn[1] = (unsigned int)(b >> 32);
542 for (int i = 2; i < WIDTH; i++)
543 pn[i] = 0;
544 }
545
546 uint256& operator=(uint64 b)
547 {
548 pn[0] = (unsigned int)b;
549 pn[1] = (unsigned int)(b >> 32);
550 for (int i = 2; i < WIDTH; i++)
551 pn[i] = 0;
552 return *this;
553 }
554
555 explicit uint256(const std::string& str)
556 {
557 SetHex(str);
558 }
559
560 explicit uint256(const std::vector<unsigned char>& vch)
561 {
562 if (vch.size() == sizeof(pn))
563 memcpy(pn, &vch[0], sizeof(pn));
564 else
565 *this = 0;
566 }
567};
568
569inline bool operator==(const uint256& a, uint64 b) { return (base_uint256)a == b; }
570inline bool operator!=(const uint256& a, uint64 b) { return (base_uint256)a != b; }
571inline const uint256 operator<<(const base_uint256& a, unsigned int shift) { return uint256(a) <<= shift; }
572inline const uint256 operator>>(const base_uint256& a, unsigned int shift) { return uint256(a) >>= shift; }
573inline const uint256 operator<<(const uint256& a, unsigned int shift) { return uint256(a) <<= shift; }
574inline const uint256 operator>>(const uint256& a, unsigned int shift) { return uint256(a) >>= shift; }
575
576inline const uint256 operator^(const base_uint256& a, const base_uint256& b) { return uint256(a) ^= b; }
577inline const uint256 operator&(const base_uint256& a, const base_uint256& b) { return uint256(a) &= b; }
578inline const uint256 operator|(const base_uint256& a, const base_uint256& b) { return uint256(a) |= b; }
579inline const uint256 operator+(const base_uint256& a, const base_uint256& b) { return uint256(a) += b; }
580inline const uint256 operator-(const base_uint256& a, const base_uint256& b) { return uint256(a) -= b; }
581
582inline bool operator<(const base_uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; }
583inline bool operator<=(const base_uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; }
584inline bool operator>(const base_uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; }
585inline bool operator>=(const base_uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; }
586inline bool operator==(const base_uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; }
587inline bool operator!=(const base_uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; }
588inline const uint256 operator^(const base_uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
589inline const uint256 operator&(const base_uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; }
590inline const uint256 operator|(const base_uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; }
591inline const uint256 operator+(const base_uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
592inline const uint256 operator-(const base_uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
593
594inline bool operator<(const uint256& a, const base_uint256& b) { return (base_uint256)a < (base_uint256)b; }
595inline bool operator<=(const uint256& a, const base_uint256& b) { return (base_uint256)a <= (base_uint256)b; }
596inline bool operator>(const uint256& a, const base_uint256& b) { return (base_uint256)a > (base_uint256)b; }
597inline bool operator>=(const uint256& a, const base_uint256& b) { return (base_uint256)a >= (base_uint256)b; }
598inline bool operator==(const uint256& a, const base_uint256& b) { return (base_uint256)a == (base_uint256)b; }
599inline bool operator!=(const uint256& a, const base_uint256& b) { return (base_uint256)a != (base_uint256)b; }
600inline const uint256 operator^(const uint256& a, const base_uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
601inline const uint256 operator&(const uint256& a, const base_uint256& b) { return (base_uint256)a & (base_uint256)b; }
602inline const uint256 operator|(const uint256& a, const base_uint256& b) { return (base_uint256)a | (base_uint256)b; }
603inline const uint256 operator+(const uint256& a, const base_uint256& b) { return (base_uint256)a + (base_uint256)b; }
604inline const uint256 operator-(const uint256& a, const base_uint256& b) { return (base_uint256)a - (base_uint256)b; }
605
606inline bool operator<(const uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; }
607inline bool operator<=(const uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; }
608inline bool operator>(const uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; }
609inline bool operator>=(const uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; }
610inline bool operator==(const uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; }
611inline bool operator!=(const uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; }
612inline const uint256 operator^(const uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
613inline const uint256 operator&(const uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; }
614inline const uint256 operator|(const uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; }
615inline const uint256 operator+(const uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
616inline const uint256 operator-(const uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
617
618
619
620
621
622
623
624
625
626
627
628
629inline int Testuint256AdHoc(vector<string> vArg)
630{
631 uint256 g(0);
632
633
634 printf("%s\n", g.ToString().c_str());
635 g--; printf("g--\n");
636 printf("%s\n", g.ToString().c_str());
637 g--; printf("g--\n");
638 printf("%s\n", g.ToString().c_str());
639 g++; printf("g++\n");
640 printf("%s\n", g.ToString().c_str());
641 g++; printf("g++\n");
642 printf("%s\n", g.ToString().c_str());
643 g++; printf("g++\n");
644 printf("%s\n", g.ToString().c_str());
645 g++; printf("g++\n");
646 printf("%s\n", g.ToString().c_str());
647
648
649
650 uint256 a(7);
651 printf("a=7\n");
652 printf("%s\n", a.ToString().c_str());
653
654 uint256 b;
655 printf("b undefined\n");
656 printf("%s\n", b.ToString().c_str());
657 int c = 3;
658
659 a = c;
660 a.pn[3] = 15;
661 printf("%s\n", a.ToString().c_str());
662 uint256 k(c);
663
664 a = 5;
665 a.pn[3] = 15;
666 printf("%s\n", a.ToString().c_str());
667 b = 1;
668 b <<= 52;
669
670 a |= b;
671
672 a ^= 0x500;
673
674 printf("a %s\n", a.ToString().c_str());
675
676 a = a | b | (uint256)0x1000;
677
678
679 printf("a %s\n", a.ToString().c_str());
680 printf("b %s\n", b.ToString().c_str());
681
682 a = 0xfffffffe;
683 a.pn[4] = 9;
684
685 printf("%s\n", a.ToString().c_str());
686 a++;
687 printf("%s\n", a.ToString().c_str());
688 a++;
689 printf("%s\n", a.ToString().c_str());
690 a++;
691 printf("%s\n", a.ToString().c_str());
692 a++;
693 printf("%s\n", a.ToString().c_str());
694
695 a--;
696 printf("%s\n", a.ToString().c_str());
697 a--;
698 printf("%s\n", a.ToString().c_str());
699 a--;
700 printf("%s\n", a.ToString().c_str());
701 uint256 d = a--;
702 printf("%s\n", d.ToString().c_str());
703 printf("%s\n", a.ToString().c_str());
704 a--;
705 printf("%s\n", a.ToString().c_str());
706 a--;
707 printf("%s\n", a.ToString().c_str());
708
709 d = a;
710
711 printf("%s\n", d.ToString().c_str());
712 for (int i = uint256::WIDTH-1; i >= 0; i--) printf("%08x", d.pn[i]); printf("\n");
713
714 uint256 neg = d;
715 neg = ~neg;
716 printf("%s\n", neg.ToString().c_str());
717
718
719 uint256 e = uint256("0xABCDEF123abcdef12345678909832180000011111111");
720 printf("\n");
721 printf("%s\n", e.ToString().c_str());
722
723
724 printf("\n");
725 uint256 x1 = uint256("0xABCDEF123abcdef12345678909832180000011111111");
726 uint256 x2;
727 printf("%s\n", x1.ToString().c_str());
728 for (int i = 0; i < 270; i += 4)
729 {
730 x2 = x1 << i;
731 printf("%s\n", x2.ToString().c_str());
732 }
733
734 printf("\n");
735 printf("%s\n", x1.ToString().c_str());
736 for (int i = 0; i < 270; i += 4)
737 {
738 x2 = x1;
739 x2 >>= i;
740 printf("%s\n", x2.ToString().c_str());
741 }
742
743
744 for (int i = 0; i < 100; i++)
745 {
746 uint256 k = (~uint256(0) >> i);
747 printf("%s\n", k.ToString().c_str());
748 }
749
750 for (int i = 0; i < 100; i++)
751 {
752 uint256 k = (~uint256(0) << i);
753 printf("%s\n", k.ToString().c_str());
754 }
755
756 return (0);
757}
This page took 0.101303 seconds and 4 git commands to generate.