E
E
eegmak2021-01-30 15:15:43
Algorithms
eegmak, 2021-01-30 15:15:43

Do you know algorithms for long arithmetic whose operations do not go beyond the boundaries of two bytes?

A long integer can be represented as an array of bytes, and then implement various operations of two arrays of bytes (multiplication, division, remainder of exponentiation, addition).
Do you know algorithms for long integers that do not overflow like equal two bytes (2 times the base of the number system) and the result of which can immediately be an array of bytes?
for example, here is the multiplication of two double-byte numbers, but the result is not an array of bytes, but an integer

n1=1023
n2=2047
n1b=n1.to_bytes(2,byteorder="big") #функция переводит число в байт массив из двух элементов, где n1b[0]*256+n1b[1]=n1
n2b=n2.to_bytes(2,byteorder="big")
zzz=n1b[1]*n2b[1]+((n1b[0]+n1b[1])*(n2b[0]+n2b[1])-n1b[0]*n2b[0]-n1b[1]*n2b[1])*256+n1b[0]*n2b[0]*256*256
#zzz= n1 умножить на n2

What approach would you recommend for such tasks?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#, 2021-01-30
@mindtester

for the result of multiplying two int32, you always need to reserve int64 and so on (8x8=>16, 16x16=>32)
ps simply consider the result of multiplication as an array of 2 elements of the base dimension

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question