x bit integer ( example 20552 bit integer value ) how do handle or create one?

Hello everyone!

I'm thinking about creating a class what can hold x bits integer in it.
There's no limit that how much bits there can be.

This is that i want to do with it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// lets assume that the myint class is that class and the value inside of <>
// is the bit amount;
myint<1024> a;
myint<2048> b;

// lets play with myint a variable like you would handle a normal int
// i know that the limit of value is 32 bits if i want to set it that way so
// this is just one example to
a = 1073741824;
a *= 16777216;
// now the a holds larger value than 32 bit int can hold
// all the default operators like /, *, +, -, % should work

// now to set variable a value bigger than 32 bit int this is what would i do:
a.set( 1234567890, 1234567890, 100123 );
// now the a variable holds the value: 12345678901234567890100123

// the function can also look like this:
a.set( 1234567890, 12345678 );
// and the value will be: 123456789012345678

// however the problem with that kind of method is that you can't
// create value like this:
a.set( 1000000000, 0000000 );
// the value will not be: 10000000000000000 because 
// param 2 is 0. Doesn't matter how many 0's there are, its still 0
// if there are no 'else value' in front

// now the class can also do
b = a * a;
// or
b += a;
// etc...

// one nice way to set the value will be:
char bytes[256];
byte[0] = 255;
byte[1] = 255;
...
byte[255] = 98;
b = byte;
// now the b have 256byte or 2048bit value in it. 


the main problem is that i have no idea how to handle
operators like *,/,% if there are more than 32 or 64 bits

Also the x root and the other function ( not sure how to call it in english )
what does the opposite.
Those as well should be included somehow.

// ==========================
Also one more important thing is that i would not create variables like 133 bits or 23 ...
the bits are 8 * x

I just don't need a value what isn't a full byte or bytes
// ==========================

Any ideas?
Thanks!
Last edited on
Do you need to handle negative numbers also?

You can do multiplication and division in a similar fashion to the way you do them by hand.

I'd store the values in an unsigned char vector.

Finally, if you just need something to do this stuff, look into existing classes like BigInt.
Topic archived. No new replies allowed.