A
A
Ashot Takiev2016-03-24 15:32:09
Programming
Ashot Takiev, 2016-03-24 15:32:09

How to implement long arithmetic?

Good afternoon.
I set out to implement long arithmetic. There are a lot of articles on the Internet about how to implement this using an array of numbers with a base that is a multiple of 10. There are no questions here, we break the number into blocks that are multiples of the selected base, and when outputting, we take into account cases when additional zeros need to be inserted.
But what if the power of two is chosen as the base? For example 2^8 .
The input is a number, for example 123456789 . There are no problems with splitting the array into cells (the low index corresponds to the low order):

12345678 = 
--------------------------------
2^0     2^8     2^16    2^24
[21]    [205]   [91]    [7]

But I don’t understand how to translate the number system back to 10 if there is a restriction on the maximum numeric type in PL (for example, the maximum numeric value can be <= 2^16 ). Therefore, simply transferring from one system to another will not work using this method:
A_0 * 2^0 + A_1 * 2^8 + A_2 * 2^16 + ... + A_n * 2 ^ (n * 8)

Tell me, please, what am I missing? As far as I know, long arithmetic is implemented in Python in such a way that the input decimal number is converted to a string of 0 and 1, and when output it is converted back.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Schullz, 2016-03-24
@Schullz

When outputting, you can first convert from a long number in base 2 to a long number in base 10

V
Vladimir Martyanov, 2016-03-24
@vilgeforce

Look at OpenSSL and how it's all implemented there. At the same time, you may not have to reinvent the wheel unnecessarily.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question