Bigint ( II )
Another way could be distributing the bits of the binary number over built-in types ( btw, from now on, I'll focus on C++ ), example:
We have a number 4903662431, which is 100100100010001111111001101011111in binary, and we distribute it over unsigned chars ( 8 bits most of the time ):
|00000001| |00100100| |01000111| |11110011| |01011111|
| char 1 | | char 2 | | char 3 | | char 4 | | char 5 |
See, we divide it in chunks of 8 bits going from the right to left.
Now we can easly add two numbers using less memory, and faster. The process is easy: you take the right most char from a number and the char in that in the same position from the other int and just add them together using the processor, since the result is moduled to the maximum size of the char ( in this case 256 ( 2^8 ) ) we dont have to worry about anything but the remainder. When you add in binary, if the result is bigger than 1 then you add add a 1 to the next digit, so same thing here, but how do we know when something is going to be bigger than 256? one option could be puting the result in a bigger variable, an int for example, but if we want to optimize our bigint to the maximum we'll want to use the biggest variable for adding. Another possibility is algebra, say we have 2 number a and b, and a third number c representing the maximum size, when is a + b >= c ? well easy: a+b >= c => a >= c-b, see, here we are not using numbers bigger than c.
So now that we have adding implemented, we can quickly make the multiplication and substraction, the multiplication of a times b is a summed b times so thats easy. And substraction is just like the sum but changing some minor differences.
tbc
We have a number 4903662431, which is 100100100010001111111001101011111in binary, and we distribute it over unsigned chars ( 8 bits most of the time ):
|00000001| |00100100| |01000111| |11110011| |01011111|
| char 1 | | char 2 | | char 3 | | char 4 | | char 5 |
See, we divide it in chunks of 8 bits going from the right to left.
Now we can easly add two numbers using less memory, and faster. The process is easy: you take the right most char from a number and the char in that in the same position from the other int and just add them together using the processor, since the result is moduled to the maximum size of the char ( in this case 256 ( 2^8 ) ) we dont have to worry about anything but the remainder. When you add in binary, if the result is bigger than 1 then you add add a 1 to the next digit, so same thing here, but how do we know when something is going to be bigger than 256? one option could be puting the result in a bigger variable, an int for example, but if we want to optimize our bigint to the maximum we'll want to use the biggest variable for adding. Another possibility is algebra, say we have 2 number a and b, and a third number c representing the maximum size, when is a + b >= c ? well easy: a+b >= c => a >= c-b, see, here we are not using numbers bigger than c.
So now that we have adding implemented, we can quickly make the multiplication and substraction, the multiplication of a times b is a summed b times so thats easy. And substraction is just like the sum but changing some minor differences.
tbc


