473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

big integers

hello,

i have to write a simple class to represent big integer numbers and
implement basic arithmetical operations such as + - * / modulo, etc.
all will be written in c++ under linux OS on x86 architecture (however
that's not that important)

could you tell me what is an idea behind such construction?
i think about creating an dynamic array in which single digits in 255 base
will be stored(one byte). it is a common solution way when dealing with
BigIntegers isnt it?

adding would be implemented by adding digits, bit by bit, but how can i
solve the problem with a carry? maybe writing accessing carry flag using
asm code is a good solution? is there a better way?

and what about multiplication? is it made by adding the number repeatedly?

maybe you have a relevant tutorial or howto :)?
--
*a*
Jul 22 '05 #1
8 7028

"adrin" <ad***@adrin.adrin> wrote in message news:cs**********@opal.futuro.pl...
hello,

i have to write a simple class to represent big integer numbers and
implement basic arithmetical operations such as + - * / modulo, etc.
all will be written in c++ under linux OS on x86 architecture (however
that's not that important)

could you tell me what is an idea behind such construction?
i think about creating an dynamic array in which single digits in 255 base
will be stored(one byte). it is a common solution way when dealing with
BigIntegers isnt it?

adding would be implemented by adding digits, bit by bit, but how can i
solve the problem with a carry? maybe writing accessing carry flag using
asm code is a good solution? is there a better way?

and what about multiplication? is it made by adding the number repeatedly?

maybe you have a relevant tutorial or howto :)?

[snip]

Look at C++ BigInt class at
http://groups-beta.google.com/group/...61372dd0c1490c

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Jul 22 '05 #2
adrin wrote:
i have to write a simple class to represent big integer numbers and
implement basic arithmetical operations such as + - * / modulo, etc.
all will be written in c++ under linux OS on x86 architecture (however
that's not that important)

could you tell me what is an idea behind such construction?
The idea is to store numbers that your architecture doesn't allow to
store using normal ways.
i think about creating an dynamic array in which single digits in 255 base
will be stored(one byte). it is a common solution way when dealing with
BigIntegers isnt it?
To know about what's common, you need to set the sampling parameters.
adding would be implemented by adding digits, bit by bit,
Why bit by bit? Couldn't you simply add two 255-base digits using normal
[unsigned] octet rules? Especially if your platform has 8-bit bytes, it
would be really simple, no?
but how can i
solve the problem with a carry?
You keep the carry flag and add it to the next pair. Just like any other
long addition/subtraction/multiplication/division...
maybe writing accessing carry flag using
asm code is a good solution? is there a better way?
You don't need asm to write C++. If I were your instructor, I would
fail you for using asm, actually.
and what about multiplication? is it made by adding the number repeatedly?
Well, sort of. For every multidigit numbers A and B you can define the
pairs of single-digit multiplications which need to be added to each other
later.
maybe you have a relevant tutorial or howto :)?


www.google.com

And come back when you have a C++ language question.

V
Jul 22 '05 #3
Alex Vinokur wrote:
http://groups-beta.google.com/group/...61372dd0c1490c


thank you very much :) ill study that code for sure

--
*a*
Jul 22 '05 #4
Victor Bazarov <v.********@comAcast.net> wrote in
news:SH*******************@newsread1.mlpsca01.us.t o.verio.net:
Why bit by bit? Couldn't you simply add two 255-base digits using normal
[unsigned] octet rules? Especially if your platform has 8-bit bytes, it
would be really simple, no?


maybe it'll sound stupid, but what do you mean by octet rules?
thanks for help,
--
*a*
Jul 22 '05 #5
adrin wrote:
Victor Bazarov <v.********@comAcast.net> wrote in
news:SH*******************@newsread1.mlpsca01.us.t o.verio.net:

Why bit by bit? Couldn't you simply add two 255-base digits using normal
[unsigned] octet rules? Especially if your platform has 8-bit bytes, it
would be really simple, no?

maybe it'll sound stupid, but what do you mean by octet rules?


The rules your compiler is likely to follow.

Your compiler is likely to have a type for unsigned char that has 8 bits.
Those are octets. Add two together in an unsigned short or an unsigned
int, or multiply them, or whatever, and your compiler will happily do it
for you (using its own rules for those operations). If your compiler does
not support 8-bit bytes, you can still use the 8 bits out of your [larger]
char and perform those operations inside some larger types.

Perhaps I used the wrong word to state the obvious :-/
Jul 22 '05 #6
adrin wrote:
hello,

i have to write a simple class to represent big integer numbers and
implement basic arithmetical operations such as + - * / modulo, etc.
all will be written in c++ under linux OS on x86 architecture (however
that's not that important)

could you tell me what is an idea behind such construction?
i think about creating an dynamic array in which single digits in 255 base
will be stored(one byte). it is a common solution way when dealing with
BigIntegers isnt it?

adding would be implemented by adding digits, bit by bit, but how can i
solve the problem with a carry? maybe writing accessing carry flag using
asm code is a good solution? is there a better way?

and what about multiplication? is it made by adding the number repeatedly?

maybe you have a relevant tutorial or howto :)?

Another advice: Use gmp
(http://www.swox.com/gmp/), which is a
big number library for c, coming along
with comfortable c++ wrapper classes
(i.e., overloaded operators etc.).

Answering your question whether
multiplication is done by adding numbers
repeatedly: for large numbers there are
much more clever ways of multiplication
(karatsuba and fft-based algorithms).
gmp also supports such fast arithmetic,
which is a good reason not do implement
things yourself - algorithms for fast
arithmetic are _very_ complicated to
implements.
Jul 22 '05 #7
Axel wrote:
[...]
Answering your question whether multiplication is done by adding numbers
repeatedly: for large numbers there are much more clever ways of
multiplication (karatsuba and fft-based algorithms). gmp also supports
such fast arithmetic, which is a good reason not do implement things
yourself - algorithms for fast arithmetic are _very_ complicated to
implements.


Which may actually defeat the purpose. I think that the original query
was because of the homework. Nobody has to (or would) "write a simple
class" when there are industrial-strength libraries lying around on the
'Net *unless* that was the actual task: to implement things yourself.

V
Jul 22 '05 #8

"adrin" <ad***@adrin.adrin> wrote in message
news:cs**********@opal.futuro.pl...
hello,

i have to write a simple class to represent big integer numbers and
implement basic arithmetical operations such as + - * / modulo, etc.
all will be written in c++ under linux OS on x86 architecture (however
that's not that important)

could you tell me what is an idea behind such construction?
i think about creating an dynamic array in which single digits in 255 base
will be stored(one byte). it is a common solution way when dealing with
BigIntegers isnt it?

adding would be implemented by adding digits, bit by bit, but how can i
solve the problem with a carry? maybe writing accessing carry flag using
asm code is a good solution? is there a better way?

and what about multiplication? is it made by adding the number repeatedly?

maybe you have a relevant tutorial or howto :)?


A better way of doing so is to use the fact that every integer can be
decomposed in a factorial sum (don't know if that is the good english term)
meaning any integer can be written like this : x1*y1! + x2*y2! + x3*y3! +
.....xn*yn!

You can then "'easily" (depends on your maths skills actually) do addition,
multiplication can be a little trickier though
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dan Stromberg | last post by:
Does anyone have a python implementation (or python C/C+ extension) that would allow me to perform set operations (union, intersection, difference, number of elements, &c) over very large...
29
by: Chris Dutrow | last post by:
I searched around on the net for a bit, couldn't find anything though. I would like to find some code for a function where I input A Range Of Integers For example: Function( 1, 100 ); And the...
13
by: Jeff Melvaine | last post by:
I note that I can write expressions like "1 << 100" and the result is stored as a long integer, which means it is stored as an integer of arbitrary length. I may need to use a large number of...
4
by: Neal Becker | last post by:
I can do this with a generator: def integers(): x = 1 while (True): yield x x += 1 for i in integers():
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
16
by: aruna | last post by:
Given a set of integers, how to write a program in C to sort these set of integers using C, given the following conditions a. Do not use arrays b. Do not use any comparison function like if/then...
1
by: calvin | last post by:
Can anyone write a code for this? Searching a set of Integers You are given two sets of integers. S1 and S2. The size of S1 is less than sizeof S2, i.e. the number of integers in S1 is less...
7
by: Girish Sahani | last post by:
Hi, Please check out the following loop,here indexList1 and indexList2 are a list of numbers. for index1 in indexList1: for index2 in indexList2: if ti1 == ti2 and not index1 !=...
7
by: mathon | last post by:
hi, i have the following recursive function: unsigned int sum_odds(unsigned int n) { if(n==1) return 1; else
9
by: Mark Morss | last post by:
I would like to construct a class that includes both the integers and None. I desire that if x and y are elements of this class, and both are integers, then arithmetic operations between them,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.