Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent a number in base 2³²?

If I have some base 10 or base 16 number, how do I change it into base 232?

The reason I'm trying to do this, is for implementing BigInt as suggested by other members here Why to use higher base for implementing BigInt?

Will it be the same as integer (base 10) till 232? What will happen after it?

like image 619
questions Avatar asked Apr 16 '12 19:04

questions


People also ask

How do you write numbers in any base?

1: The base of any number may be written beside the number. For example, 178 is read as 17 base 8, which is 15 in base 10. Example 7.2.

How do you write a number in base 2?

In binary, base 2, there are only two digits (0 and 1), and each place is worth two times the place to its right. The subscript 2 on 11012 means the 1101 is in base 2. Numbers are normally written in base 10, so a subscript 10 is only used when needed for clarity.

How to convert 32 to base 2?

32 in binary is 100000. To find decimal to binary equivalent, divide 32 successively by 2 until the quotient becomes 0. The binary equivalent can be obtained by writing the remainder in each division step from the bottom to the top.


1 Answers

You are trying to find something of the form

a0 + a1 * (2^32) + a2 * (2^32)^2 + a3 * (2^32)^3 + ...

which is exactly the definition of a base-232 system, so ignore all the people that told you that your question doesn't make sense!

Anyway, what you are describing is known as base conversion. There are quick ways and there are easy ways to solve this. The quick ways are very complicated (there are entire chapters of books dedicated to the subject), and I'm not going to attempt to address them here (not least because I've never attempted to use them).

One easy way is to first implement two functions in your number system, multiplication and addition. (i.e. implement BigInt add(BigInt a, BigInt b) and BigInt mul(BigInt a, BigInt b)). Once you've solved that, you will notice that a base-10 number can be expressed as:

b0 + b1 * 10 + b2 * 10^2 + b3 * 10^3 + ...

which can also be written as:

b0 + 10 * (b1 + 10 * (b2 + 10 * (b3 + ...

so if you move left-to-right in your input string, you can peel off one base-10 digit at a time, and use your add and mul functions to accumulate into your BigInt:

BigInt a = 0;
for each digit b {
    a = add(mul(a, 10), b);
}

Disclaimer: This method is not computationally efficient, but it will at least get you started.

Note: Converting from base-16 is much simpler, because 232 is an exact multiple of 16. So the conversion basically comes down to concatenating bits.

like image 197
Oliver Charlesworth Avatar answered Sep 30 '22 13:09

Oliver Charlesworth