0

I'm implementing karatsuba's method as part of an exercise. Karatsuba's method itself isn't terribly difficult, but one part of it is confusing me. Both numbers being multiplied have to be split into two halves, the high and the low bits. But I can't find much information about how this split is done.

I noticed most Karatsuba implementations use strings to represent huge numbers, but I'm doing something a bit different. I'm representing them as an array of ints, where each element is the next 30 bits of the huge number. Note that this means these arrays may be odd-length. If the huge number's size is not a multiple of 30, it gets leading zeros so it can still be represented as such.

So how can this be split into high and low halves? The main problem I'm running into is that since it can be odd-length, that means I can't just divide the arrays by their elements. Basically, how can I select the first and last bit halves of these int arrays so I can continue recursing in Karatsuba's method?

As long as I can retrieve the bits, I can create two smaller int arrays from them.

Bob
  • 715
  • 2
  • 11
  • 32
  • split the array in two, if there is an odd number of int, left shift then right shift the middle int by 16 to get the left half and right shift by 16 to get the right half? 15 if you only use 30 bits to code each int (even if it is quite strange) – Nyashes Mar 20 '17 at 22:18
  • How can I split the array in two if there are an odd number of elements? – Bob Mar 20 '17 at 22:28
  • 21 elements, 10 on the right, 10 on the left, cut the middle one with bitshifts as described in my previous comment – Nyashes Mar 20 '17 at 23:11
  • Thanks, that helps. But in your first comment, I think you mixed up the right and left half. Shifting left then right would get the right have, and shifting just right would get the left half I think – Bob Mar 21 '17 at 00:46
  • Yup. I indeed mixed it up – Nyashes Mar 21 '17 at 00:52

0 Answers0