If you really don't need arbitrary precision, then take advantage of the runtime. I'm not sure if a lookup table approach is possible or not. I wrote this before your clarifying comment so it probably isn't quite is applicable. Then, you are only reduced to adding n numbers, rather than multiplying and adding n numbers (plus the cost of the memory of the lookup table) The best you can do is have a lookup table of the form: (a,b) where a is the digit position, and b is the digit (0.9). You'll notice that the multiplication never gets simple, so you can't have any lookup tables and do bitshifts and ors, no matter how big you group them. So if you have the lookup table of A = (0b000 to 0b111), then the multiplication is always by 1 and some trailing zeros, so the multiplication is simple (just shifting left). The reason that it is possible for base 8 (and 16) is that the way the conversion works is following: This is not possible in bases that aren't powers of two to convert to base-2. I am aware of the multiply and add solution but since these are arbitrary length numbers, the multiply and add operations are not free so I'd like to avoid them, if at all possible. I know that for base 10, you can't just give it one digit at a time, so the solution would likely have to lookup a group of digits at a time. I was curious if anyone had any clever way to figure out how to generate a generic look up table for Base X -> Base 2. It's definitely not so obvious with base 10, though. I know that I could write the algorithm like atoi does and do a bunch of multiplies and adds, but for this specific problem I'm trying to see if I can do it with a look up table. However, my problem is that I want to do this look up table method for odd bases, like base 10. I can simply use a lookup table for each digit to get a series of bits. I have found that this is simple for binary, octal, and hexadecimal. My solution now does what the atoi() function does, but I was curious purely out of academic interest if a lookup table solution is possible. Java actuall uses an general algorithm to convert any radix other than decimal conversion, that involves calculation of how many digits (of the given radix) per int.I want to be able to take, as input, a character pointer to a number in base 2 through 16 and as a second parameter, what base the number is in and then convert that to it's representation in base 2. That's why they choose a base 10^9 to store that. Easily add digits of that base using int without overflow (should be less than or equal to half of 2^31).Should take nearly all the bits of an int into account) Nearly fit to an int max value (if less than that too much, you will be wasting memory.To store efficiently, Java should choose a very big base to: In whatever base, that value remains the same. You have a number value, and you can store it in any base: base 2 (binary), base 10 (decimal), base 16 (Hex) or base 100. To be correct, Java stores BigInteger as array of int (Java doesn't have unsigned int). The loop works like this: number: 1234 (base 10) Return the intermediate BigInteger as function result.Clean up and if necessary, remove leading zeroes, etc.Repeat from point 2 until the entire string is read.Multiply the intermediate result by the base and add the value of the digit, so if base is 10, multiply the intermediate result by 10 and add the 9 you just read.in base 10, '9' is converted to 9, in base 16, 'A' is converted to 10, etc. using the given numeric base (radix, 2.36), convert the digit to binary, e.g.read the sign (if there is any) and remember it.Set the intermediate result (a BigInteger) to value BigInteger.ZERO.For very large numbers, perhaps a tricky divide-and-conquer algorithm is used, but otherwise, it is done like for integers:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |