-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbig_integer
786 lines (700 loc) · 31.4 KB
/
big_integer
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_MATH_BIG_INTEGER_HPP
#define GTL_MATH_BIG_INTEGER_HPP
// Summary: Arbitrary sized signed integers.
#ifndef NDEBUG
#if defined(_MSC_VER)
#define __builtin_trap() __debugbreak()
#endif
/// @brief A simple assert macro to break the program if the big_integer is misused.
#define GTL_BIG_INTEGER_ASSERT(ASSERTION, MESSAGE) static_cast<void>((ASSERTION) || (__builtin_trap(), 0))
#else
/// @brief At release time the assert macro is implemented as a nop.
#define GTL_BIG_INTEGER_ASSERT(ASSERTION, MESSAGE) static_cast<void>(0)
#endif
#include <math/big_unsigned>
namespace gtl {
class big_integer final {
private:
signed int sign;
big_unsigned magnitude;
public:
/// @brief Empty constructor initialises the big number with the value of zero.
big_integer()
: sign(0)
, magnitude(0) {
}
/// @brief Conversion constructor from a bool value.
/// @param value The value to store.
big_integer(const bool value)
: sign(value)
, magnitude(value) {
}
/// @brief Conversion constructor from a char value.
/// @param value The value to store.
big_integer(const char value)
: sign((value > 0) - (value < 0))
, magnitude(0) {
if (value < 0) {
this->magnitude = -(value + 1);
++this->magnitude;
}
else {
this->magnitude = value;
}
}
/// @brief Conversion constructor from a signed char value.
/// @param value The value to store.
big_integer(const signed char value)
: sign((value > 0) - (value < 0))
, magnitude(0) {
if (value < 0) {
this->magnitude = -(value + 1);
++this->magnitude;
}
else {
this->magnitude = value;
}
}
/// @brief Conversion constructor from a signed short value.
/// @param value The value to store.
big_integer(const signed short value)
: sign((value > 0) - (value < 0))
, magnitude(0) {
if (value < 0) {
this->magnitude = -(value + 1);
++this->magnitude;
}
else {
this->magnitude = value;
}
}
/// @brief Conversion constructor from a signed int value.
/// @param value The value to store.
big_integer(const signed int value)
: sign((value > 0) - (value < 0))
, magnitude(0) {
if (value < 0) {
this->magnitude = -(value + 1);
++this->magnitude;
}
else {
this->magnitude = value;
}
}
// No constructor for "signed long int" as these can be 32 or 64 bit depending on platform.
/// @brief Conversion constructor from a signed long long int value.
/// @param value The value to store.
big_integer(const signed long long int value)
: sign((value > 0) - (value < 0))
, magnitude(0) {
if (value < 0) {
this->magnitude = -(value + 1);
++this->magnitude;
}
else {
this->magnitude = value;
}
}
/// @brief Conversion constructor from an unsigned char value.
/// @param value The value to store.
big_integer(const unsigned char value)
: sign(value > 0)
, magnitude(value) {
}
/// @brief Conversion constructor from an unsigned short value.
/// @param value The value to store.
big_integer(const unsigned short value)
: sign(value > 0)
, magnitude(value) {
}
/// @brief Conversion constructor from an unsigned int value.
/// @param value The value to store.
big_integer(const unsigned int value)
: sign(value > 0)
, magnitude(value) {
}
// No constructor for "unsigned long int" as these can be 32 or 64 bit depending on platform.
/// @brief Conversion constructor from an unsigned long long int value.
/// @param value The value to store.
big_integer(const unsigned long long int value)
: sign(value > 0)
, magnitude(value) {
}
/// @brief Conversion constructor from an unsigned big number.
/// @param value The value to store.
big_integer(const big_unsigned& value)
: sign(value > 0)
, magnitude(value) {
}
/// @brief Conversion constructor from a string value in base 10.
/// @param value The value to store.
/// @param length The length of the string.
big_integer(const char* value, const unsigned long long int length)
: sign(0)
, magnitude(0) {
// Sanity check length.
if (length == 0) {
return;
}
GTL_BIG_INTEGER_ASSERT(value != nullptr, "Value must not be null with a length greater than zero.");
GTL_BIG_INTEGER_ASSERT((((value[0] >= '0') && (value[0] <= '9')) || (value[0] == '+') || (value[0] == '-')), "Value must start with an integer or sign character.");
// Check for a sign character.
if (value[0] == '+') {
this->sign = 1;
}
else if (value[0] == '-') {
this->sign = -1;
}
// Consume the remaining string as the magnitude.
this->magnitude = big_unsigned(&value[(this->sign != 0)], length - (this->sign != 0));
// If the magniture is zero the sign must be zero.
if (this->magnitude == 0) {
this->sign = 0;
}
// Otherwise if the sign hasn't been set ensure it is positive.
else if (this->sign == 0) {
this->sign = 1;
}
}
/// @brief Copy constructor from another big number.
/// @param other The other big number to copy.
big_integer(const big_integer& other)
: sign(other.sign)
, magnitude(other.magnitude) {
}
/// @brief Move constructor from another big number.
/// @param other The other big number to move.
big_integer(big_integer&& other)
: sign(other.sign)
, magnitude(other.magnitude) {
}
/// @brief Copy assignment operator from another big number.
/// @param other The other big number to copy.
/// @return A reference to this.
big_integer& operator=(const big_integer& other) {
if (this != &other) {
this->sign = other.sign;
this->magnitude = other.magnitude;
}
return *this;
}
/// @brief Move assignment operator from another big number.
/// @param other The other big number to move.
/// @return A reference to this.
big_integer& operator=(big_integer&& other) {
if (this != &other) {
this->sign = other.sign;
this->magnitude = other.magnitude;
}
return *this;
}
public:
/// @brief Conversion operator to check if the big number is zero.
/// @return true if the big number is not zero, false otherwise.
explicit operator bool() const {
return (this->sign != 0);
}
public:
/// @brief Unary plus operator returns a copy of the big number.
big_integer operator+() const {
return *this;
}
/// @brief Unary minus operator returns a copy of the big number with the sign inverted;
big_integer operator-() const {
big_integer inverted(*this);
inverted.sign = -this->sign;
return inverted;
}
public:
/// @brief Pre-increment operator, add one to the big number and return it.
/// @return A reference to this after the increment.
big_integer& operator++() {
if (this->sign < 0) {
if (--this->magnitude == 0) {
this->sign = 0;
}
}
else {
this->sign = 1;
++this->magnitude;
}
return *this;
}
/// @brief Post-increment operator, add one to the big number and return a copy from before the increment.
/// @return The value of the big number before the increment.
big_integer operator++(int) {
big_integer temp(*this);
++(*this);
return temp;
}
/// @brief Pre-decrement operator, subtract one from the big number and return it.
/// @return A reference to this after the decrement.
big_integer& operator--() {
if (this->sign > 0) {
if (--this->magnitude == 0) {
sign = 0;
}
}
else {
this->sign = -1;
++this->magnitude;
}
return *this;
}
/// @brief Post-decrement operator, subtract one from the big number and return a copy from before the decrement.
/// @return The value of the big number before the decrement.
big_integer operator--(int) {
big_integer temp(*this);
--(*this);
return temp;
}
public:
/// @brief Add two big numbers together.
/// @param lhs The left hand side of the expression.
/// @param rhs The right hand side of the expression.
/// @return A big number with the value of the result of the evaluated expression.
static big_integer add(const big_integer& lhs, const big_integer& rhs) {
if (lhs.sign == 0) {
return rhs;
}
if (rhs.sign == 0) {
return lhs;
}
if (lhs.sign == rhs.sign) {
big_integer result;
result.sign = lhs.sign;
result.magnitude = lhs.magnitude + rhs.magnitude;
return result;
}
if (rhs.magnitude == lhs.magnitude) {
return {};
}
if (rhs.magnitude < lhs.magnitude) {
big_integer result;
result.sign = lhs.sign;
result.magnitude = lhs.magnitude - rhs.magnitude;
return result;
}
big_integer result;
result.sign = rhs.sign;
result.magnitude = rhs.magnitude - lhs.magnitude;
return result;
}
/// @brief Subtract a big number from another.
/// @param lhs The left hand side of the expression.
/// @param rhs The right hand side of the expression.
/// @return A big number with the value of the result of the evaluated expression.
static big_integer subtract(const big_integer& lhs, const big_integer& rhs) {
return big_integer::add(lhs, -rhs);
}
/// @brief Multiply two big numbers together.
/// @param lhs The left hand side of the expression.
/// @param rhs The right hand side of the expression.
/// @return A big number with the value of the result of the evaluated expression.
static big_integer multiply(const big_integer& lhs, const big_integer& rhs) {
big_integer result;
result.sign = lhs.sign * rhs.sign;
result.magnitude = lhs.magnitude * rhs.magnitude;
return result;
}
/// @brief Divide a big number by another.
/// @param lhs The left hand side of the expression.
/// @param rhs The right hand side of the expression.
/// @return A big number with the value of the result of the evaluated expression.
static big_integer divide(const big_integer& lhs, const big_integer& rhs) {
big_integer result;
result.sign = lhs.sign * rhs.sign;
result.magnitude = lhs.magnitude / rhs.magnitude;
if (result.magnitude == 0) {
result.sign = 0;
}
return result;
}
/// @brief Bitwise shift a big number left by a number of bits.
/// @param lhs The left hand side of the expression.
/// @param rhs The number of bits to shift the big number by.
/// @return A big number with the value of the result of the evaluated expression.
static big_integer bit_shift_left(const big_integer& lhs, const unsigned long long int rhs) {
big_integer result;
result.sign = lhs.sign;
result.magnitude = lhs.magnitude << rhs;
return result;
}
/// @brief Bitwise shift a big number right by a number of bits.
/// @param lhs The left hand side of the expression.
/// @param rhs The number of bits to shift the big number by.
/// @return A big number with the value of the result of the evaluated expression.
static big_integer bit_shift_right(const big_integer& lhs, const unsigned long long int rhs) {
big_integer result;
result.sign = lhs.sign;
result.magnitude = lhs.magnitude >> rhs;
if (result.magnitude == 0) {
result.sign = 0;
}
return result;
}
/// @brief Bitwise AND two big numbers together.
/// @param lhs The left hand side of the expression.
/// @param rhs The right hand side of the expression.
/// @return A big number with the value of the result of the evaluated expression.
static big_integer bit_and(const big_integer& lhs, const big_integer& rhs) {
if ((lhs.sign < 0) && (rhs.sign < 0)) {
big_integer result;
result.sign = -1;
result.magnitude = ((lhs.magnitude - 1) | (rhs.magnitude - 1)) + 1;
return result;
}
if ((lhs.sign > 0) && (rhs.sign < 0)) {
big_integer result;
result.magnitude = big_unsigned::bit_and_not(lhs.magnitude, rhs.magnitude - 1);
result.sign = (result.magnitude == 0 ? 0 : 1);
return result;
}
if ((lhs.sign < 0) && (rhs.sign > 0)) {
big_integer result;
result.magnitude = big_unsigned::bit_and_not(rhs.magnitude, lhs.magnitude - 1);
result.sign = (result.magnitude == 0 ? 0 : 1);
return result;
}
big_integer result;
result.magnitude = lhs.magnitude & rhs.magnitude;
result.sign = (result.magnitude == 0 ? 0 : 1);
return result;
}
/// @brief Bitwise OR two big numbers together.
/// @param lhs The left hand side of the expression.
/// @param rhs The right hand side of the expression.
/// @return A big number with the value of the result of the evaluated expression.
static big_integer bit_or(const big_integer& lhs, const big_integer& rhs) {
if ((lhs.sign < 0) && (rhs.sign < 0)) {
big_integer result;
result.sign = -1;
result.magnitude = ((lhs.magnitude - 1) & (rhs.magnitude - 1)) + 1;
return result;
}
if ((lhs.sign > 0) && (rhs.sign < 0)) {
big_integer result;
result.sign = -1;
result.magnitude = big_unsigned::bit_and_not(rhs.magnitude - 1, lhs.magnitude) + 1;
return result;
}
if ((lhs.sign < 0) && (rhs.sign > 0)) {
big_integer result;
result.sign = -1;
result.magnitude = big_unsigned::bit_and_not(lhs.magnitude - 1, rhs.magnitude) + 1;
return result;
}
big_integer result;
result.sign = lhs.sign | rhs.sign;
result.magnitude = lhs.magnitude | rhs.magnitude;
return result;
}
/// @brief Bitwise XOR two big numbers together.
/// @param lhs The left hand side of the expression.
/// @param rhs The right hand side of the expression.
/// @return A big number with the value of the result of the evaluated expression.
static big_integer bit_xor(const big_integer& lhs, const big_integer& rhs) {
if ((lhs.sign < 0) && (rhs.sign < 0)) {
big_integer result;
result.magnitude = ((lhs.magnitude - 1) ^ (rhs.magnitude - 1));
result.sign = (result.magnitude == 0 ? 0 : 1);
return result;
}
if ((lhs.sign > 0) && (rhs.sign < 0)) {
big_integer result;
result.sign = -1;
result.magnitude = (lhs.magnitude ^ (rhs.magnitude - 1)) + 1;
return result;
}
if ((lhs.sign < 0) && (rhs.sign > 0)) {
big_integer result;
result.sign = -1;
result.magnitude = ((lhs.magnitude - 1) ^ rhs.magnitude) + 1;
return result;
}
big_integer result;
result.sign = lhs.sign | rhs.sign;
result.magnitude = lhs.magnitude ^ rhs.magnitude;
if (result.magnitude == 0) {
result.sign = 0;
}
return result;
}
/// @brief Bitwise NOT a big number.
/// @param lhs The left hand side of the big number.
/// @return A big number with the value of the result of the evaluated expression.
static big_integer bit_not(const big_integer& lhs) {
return ((-lhs) - 1);
}
/// @brief Compare the size of two big numbers.
/// @param lhs The left hand side of the expression.
/// @param rhs The right hand side of the expression.
/// @return true if the lhs is smaller than the rhs, false otherwise.
static bool less_than(const big_integer& lhs, const big_integer& rhs) {
return ((lhs.sign < rhs.sign) || ((lhs.sign == rhs.sign) && ((lhs.sign < 0) ? (rhs.magnitude < lhs.magnitude) : (lhs.magnitude < rhs.magnitude))));
}
/// @brief Compare the size of two big numbers.
/// @param lhs The left hand side of the expression.
/// @param rhs The right hand side of the expression.
/// @return true if the lhs is equal to the rhs, false otherwise.
static bool equal_to(const big_integer& lhs, const big_integer& rhs) {
return ((lhs.sign == rhs.sign) && (lhs.magnitude == rhs.magnitude));
}
/// @brief Compare the size of two big numbers.
/// @param lhs The left hand side of the expression.
/// @param rhs The right hand side of the expression.
/// @return true if the lhs is not equal to the rhs, false otherwise.
static bool not_equal_to(const big_integer& lhs, const big_integer& rhs) {
return ((lhs.sign != rhs.sign) || (lhs.magnitude != rhs.magnitude));
}
public:
/// @brief Get the sign of the big number as represented as a signed number.
/// @param value The big number to process.
/// @return Plus one if value is positive, zero if value is zero, or negative one if value is negative.
static signed int signum(const big_integer& value) {
return value.sign;
}
/// @brief Get the magnitude of the big number as represented as an unsigned big number.
/// @param value The big number to process.
/// @return The magnitude of the big number as an unsigned big number.
static big_unsigned abs(const big_integer& value) {
return value.magnitude;
}
public:
/// @brief Addition assignment operator.
/// @param rhs The right hand side of the expression.
/// @return A reference to this after the addition of the rhs.
big_integer& operator+=(const big_integer& rhs) {
return (*this = big_integer::add(*this, rhs));
}
/// @brief Addition operator.
/// @param rhs The right hand side of the expression.
/// @return The value of this big number added to the rhs.
big_integer operator+(const big_integer& rhs) const {
return big_integer::add(*this, rhs);
}
public:
/// @brief Subtraction assignment operator.
/// @param rhs The right hand side of the expression.
/// @return A reference to this after the subtraction of the rhs.
big_integer& operator-=(const big_integer& rhs) {
return (*this = big_integer::subtract(*this, rhs));
}
/// @brief Subtraction assignment operator.
/// @param rhs The right hand side of the expression.
/// @return A reference to this after the subtraction of the rhs.
big_integer operator-(const big_integer& rhs) const {
return big_integer::subtract(*this, rhs);
}
public:
/// @brief Multiplication assignment operator.
/// @param rhs The right hand side of the expression.
/// @return A reference to this after multiplication by the rhs.
big_integer& operator*=(const big_integer& rhs) {
return (*this = big_integer::multiply(*this, rhs));
}
/// @brief Multiplication operator.
/// @param rhs The right hand side of the expression.
/// @return The value of this big numbermultiplied by the rhs.
big_integer operator*(const big_integer& rhs) const {
return big_integer::multiply(*this, rhs);
}
public:
/// @brief Division assignment operator.
/// @param rhs The right hand side of the expression.
/// @return A reference to this after division by the rhs.
big_integer& operator/=(const big_integer& rhs) {
return (*this = big_integer::divide(*this, rhs));
}
/// @brief Division operator.
/// @param rhs The right hand side of the expression.
/// @return The value of this big number divided by the rhs.
big_integer operator/(const big_integer& rhs) const {
return big_integer::divide(*this, rhs);
}
public:
/// @brief Modulo assignment operator.
/// @param rhs The right hand side of the expression.
/// @return A reference to this, set to the remainder of this big number divided by the rhs.
big_integer& operator%=(const big_integer& rhs) {
this->magnitude %= rhs.magnitude;
if (this->magnitude == 0) {
this->sign = 0;
}
return *this;
}
/// @brief Modulo operator.
/// @param rhs The right hand side of the expression.
/// @return The remainder of this big number divided by the rhs.
big_integer operator%(const big_integer& rhs) const {
big_integer remainder;
remainder.sign = this->sign;
remainder.magnitude = this->magnitude % rhs.magnitude;
if (remainder.magnitude == 0) {
remainder.sign = 0;
}
return remainder;
}
public:
/// @brief Left bit shift assignment operator.
/// @param rhs The number of bits to shift the big number.
/// @return A reference to this after bit shifting by the rhs number of bits.
big_integer& operator<<=(unsigned long long int rhs) {
return (*this = big_integer::bit_shift_left(*this, rhs));
}
/// @brief Left bit shift operator.
/// @param rhs The number of bits to shift the big number.
/// @return The value of this big number bit shifted by the rhs number of bits.
big_integer operator<<(unsigned long long int rhs) const {
return big_integer::bit_shift_left(*this, rhs);
}
public:
/// @brief Right bit shift assignment operator.
/// @param rhs The number of bits to shift the big number.
/// @return A reference to this after bit shifting by the rhs number of bits.
big_integer& operator>>=(unsigned long long int rhs) {
return (*this = big_integer::bit_shift_right(*this, rhs));
}
/// @brief Right bit shift operator.
/// @param rhs The number of bits to shift the big number.
/// @return The value of this big number bit shifted by the rhs number of bits.
big_integer operator>>(unsigned long long int rhs) const {
return big_integer::bit_shift_right(*this, rhs);
}
public:
/// @brief Bitwise AND assignment operator.
/// @param rhs The right hand side of the expression.
/// @return A reference to this after a bitwise AND with the rhs.
big_integer& operator&=(const big_integer& rhs) {
return (*this = big_integer::bit_and(*this, rhs));
}
/// @brief Bitwise AND operator.
/// @param rhs The right hand side of the expression.
/// @return The value of this big number bitwise AND with the rhs.
big_integer operator&(const big_integer& rhs) const {
return big_integer::bit_and(*this, rhs);
}
public:
/// @brief Bitwise OR assignment operator.
/// @param rhs The right hand side of the expression.
/// @return A reference to this after a bitwise OR with the rhs.
big_integer& operator|=(const big_integer& rhs) {
return (*this = big_integer::bit_or(*this, rhs));
}
/// @brief Bitwise OR operator.
/// @param rhs The right hand side of the expression.
/// @return The value of this big number bitwise OR with the rhs.
big_integer operator|(const big_integer& rhs) const {
return big_integer::bit_or(*this, rhs);
}
public:
/// @brief Bitwise XOR assignment operator.
/// @param rhs The right hand side of the expression.
/// @return A reference to this after a bitwise XOR with the rhs.
big_integer& operator^=(const big_integer& rhs) {
return (*this = big_integer::bit_xor(*this, rhs));
}
/// @brief Bitwise XOR operator.
/// @param rhs The right hand side of the expression.
/// @return The value of this big number bitwise XOR with the rhs.
big_integer operator^(const big_integer& rhs) const {
return big_integer::bit_xor(*this, rhs);
}
public:
/// @brief Bitwise NOT operator.
/// @return The value of this big number with all its bits inverted.
big_integer operator~() {
return big_integer::bit_not(*this);
}
public:
/// @brief Less than comparison.
/// @param rhs The right hand side of the expression.
/// @return true if the value of this big number is less than the rhs, false otherwise.
bool operator<(const big_integer& rhs) const {
return big_integer::less_than(*this, rhs);
}
/// @brief Greater than comparison.
/// @param rhs The right hand side of the expression.
/// @return true if the value of this big number is greater than the rhs, false otherwise.
bool operator>(const big_integer& rhs) const {
return big_integer::less_than(rhs, *this);
}
public:
/// @brief Less than or equal comparison.
/// @param rhs The right hand side of the expression.
/// @return true if the value of this big number is less than or equal to the rhs, false otherwise.
bool operator<=(const big_integer& rhs) const {
return !big_integer::less_than(rhs, *this);
}
/// @brief Greater than or equal comparison.
/// @param rhs The right hand side of the expression.
/// @return true if the value of this big number is greater than or equal to the rhs, false otherwise.
bool operator>=(const big_integer& rhs) const {
return !big_integer::less_than(*this, rhs);
}
public:
/// @brief Equal comparison.
/// @param rhs The right hand side of the expression.
/// @return true if the value of this big number is equal to the rhs, false otherwise.
bool operator==(const big_integer& rhs) const {
return big_integer::equal_to(*this, rhs);
}
/// @brief Not equal comparison.
/// @param rhs The right hand side of the expression.
/// @return true if the value of this big number is not equal to the rhs, false otherwise.
bool operator!=(const big_integer& rhs) const {
return big_integer::not_equal_to(*this, rhs);
}
public:
/// @brief Get the length of the big number in allocated bits.
/// @return The number of bits allocated to store the big number.
/// @note This does not include a bit to store the sign.
unsigned long long int get_length_bits() const {
return this->magnitude.get_length_bits();
}
/// @brief Get the length of the big number in bytes.
/// @return The number of bytes needed to store the big number.
/// @note This does not include a byte to store the sign.
unsigned long long int get_length_bytes() const {
return this->magnitude.get_length_bytes();
}
/// @brief Get the length of the big number as a decimal number.
/// @return The number of digits required to represent the big number.
/// @note This does not include a character to store the sign.
unsigned long long int get_length_decimal() const {
return this->magnitude.get_length_decimal();
}
public:
/// @brief Convert the big number to a string and output it in the provided buffer.
/// @param buffer The output string buffer, must be larger than the decimal length of the big number plus one for null termination and one for a sign.
/// @param length The length of the output buffer.
/// @return The decimal length of the big number, equal to the number of digits written.
unsigned long long int to_string(char* buffer, const unsigned long long int length) const {
GTL_BIG_INTEGER_ASSERT(length > this->magnitude.get_length_decimal() + 1, "Buffer length must be greater than decimal length of number including space for a sign character.");
if (this->sign < 0) {
buffer[0] = '-';
}
return this->magnitude.to_string(&buffer[(this->sign < 0)], length - (this->sign < 0)) + (this->sign < 0);
}
/// @brief Convert the big number to an integer and return it.
/// @return The integer value of the big number.
int to_integer() const {
return this->sign * static_cast<int>(this->magnitude.to_unsigned_integer());
}
};
}
#undef GTL_BIG_INTEGER_ASSERT
#endif // GTL_MATH_BIG_INTEGER_HPP