PDA

View Full Version : combining bits



OgreProgrammer
January 1st, 2010, 02:14 PM
How are two 64 bit numbers combined to simulate a 128 bit number?

I've always wondered ever since my 16 bit 286+ recorded scores in 32 ranges.

Solve my 20+ year quest?

scragar
January 1st, 2010, 02:31 PM
http://gmplib.org/

It's often a better idea to use just a dedicated library for such things, that way upgrading the library as things change is easy, as opposed to strugling to find your own answers and keep them efficient/up to date.

OgreProgrammer
January 1st, 2010, 02:50 PM
Thank you.

I agree, but its purely about knowing.

It is nosiness drives me to program.

Arndt
January 1st, 2010, 02:53 PM
How are two 64 bit numbers combined to simulate a 128 bit number?

I've always wondered ever since my 16 bit 286+ recorded scores in 32 ranges.

Solve my 20+ year quest?

How do you mean, "how"? You take two 64-bit numbers, A and B, and you say that A is the higher 64 bits and B is the lower 64 bits of the 128 bit number. Then you implement the arithmetic operations accordingly.

OgreProgrammer
January 1st, 2010, 04:56 PM
How do you mean, "how"? You take two 64-bit numbers, A and B, and you say that A is the higher 64 bits and B is the lower 64 bits of the 128 bit number. Then you implement the arithmetic operations accordingly.

I know that from registers in assembler, but I am looking for information on how they catch overflow digits in multiplications and whatnot.

scragar
January 1st, 2010, 05:26 PM
I know that from registers in assembler, but I am looking for information on how they catch overflow digits in multiplications and whatnot.

I don't actually think that's what is done in most libraries, I assume they simulate the process by manipulating binary using the same rules that your processor would do for the same calculation, for example addition would work something like(psuedocode):

function add(BigInt one, BigInt two){
BigInt retObj;
bool overflow = 0;
char tmp;
for(int i = one.bits.length - 1; i >= 0; i--){
tmp = one.bits[i] + two.bits[i] + overflow;
retObj.bits[i] = tmp%2;// only the odd data
overflow = (tmp >= 2);
}
retObj.overflow = overflow;
return retObj;
}
Obviously this code is both inefficient and takes some shortcuts, but I trust you can work that out on your own. I also don't fully understand the mechanism for converting these numbers back into decimal, I imagine that would be a rather hard goal.

EDIT: in my code I keep note of my own overflow flag for warning the user, this in C++ would be very inefficient in terms of space unless you used char types for your binary data, rather than an array of ints(or better still, an array of bools. that way you can specify a nice working size to go with the excess bool)

MadCow108
January 1st, 2010, 08:20 PM
I know that from registers in assembler, but I am looking for information on how they catch overflow digits in multiplications and whatnot.

you either don't (e.g. standard 64 bit integers on 32 bit systems)
or you check for overflow on each operation and increase the size of your number.
To reduce operations you can represent your number in a high base thus reducing the number of basic types needed to represent a single number.

OgreProgrammer
January 2nd, 2010, 12:02 AM
Thanks scragar and madcow108. That function is excellent and the idea of encoding in some high base is very novel too. I can now see how it would be done.

Thanks everyone. I got a little bit from you all. And thats a lot.

Maybe now the nightmares wont come.... :D