I am currently stuck on an assignment and have looked nearly everywhere for even a hint at what I am trying to do.
The assignment is simple, we are to be given a binary number in the form of a vector (e.g. [1,1,1,1] and we are to compute the decimal form of this number and put in back into the same vector form (e.g. [1,5] for the answer to the previous example).
While at first I thought this would have an easy solution, I soon found that we are to use this method to calculate extremely large numbers such as 300 1's in binary.
Now after I realized my mistake of trying to straight up calculate it, I soon found the "divide-and-conquer" method idea but I did not find a single place that gave a precise example of how to use it in this context.
Since this is an assignment, I would rather an answer be proposed that actually explains the concept and provides examples rather than a straight up block of code.
Thank you in advance,
Matthew
Following is the solution to your problem:
The names are self explanatory. here are the steps performed:
Write a base 10 math engine.
It should include addition by another base 10 number, and multiplication by
int
. (well doubling is enough)Iterate over the binary digits, keeping track of a base 10 number that corresponds to the digit.
Conditionally accumulate.
The only hard part is the base 10 math system, everything else takes 3 to 8 lines of code.
Sadly, there is very limited easy ways to do thus more efficiently, as any digit of a binary number can influence any digit of the equivalent base 10 number. There may be fancy ways, but for 300 digits you should not bother.