How slow the 64 bit integer calculation?

L

linq936

Hi,
I am writing my own integer data type because I need to check
overflow. For example, if I have 2 very big unsigned int to add up,
there might be overflow happening. So i will have my own integer type
and override operator + and *.

All the data I will operate are 32 bit integer, so inside my data
type, I can use "unsigned int". Or I can use "unsigned long". If I use
"unsigned int", I need to check if the sum is lower than the operand.
If I use "unsigned long", I need to check if the sum is larger than
0x1_0000_0000.

But my concern is since our computers are all 32 bit, will that slow
the program a lot if I use "unsigned long"?

My code is not scientific purpose, but it has a lot data calculation,
and code speed is a concern.

Thanks.
 
L

loufoque

(e-mail address removed) a écrit :
All the data I will operate are 32 bit integer, so inside my data
type, I can use "unsigned int". Or I can use "unsigned long". If I use
"unsigned int", I need to check if the sum is lower than the operand.
If I use "unsigned long", I need to check if the sum is larger than
0x1_0000_0000.

unsigned long is 32 bits on my machine, just like unsigned int.
 
V

Victor Bazarov

I am writing my own integer data type because I need to check
overflow. For example, if I have 2 very big unsigned int to add up,
there might be overflow happening. So i will have my own integer type
and override operator + and *.

There is no need to employ a larger type to find out if addition or
multiplication might overflow.

int a, b;
// give 'a' and 'b' values
bool addition_will_overflow = INT_MAX - a < b;
bool multipliation_will_overflow = INT_MAX / a < b;

V
 
A

Alf P. Steinbach

* Victor Bazarov:
There is no need to employ a larger type to find out if addition or
multiplication might overflow.

int a, b;
// give 'a' and 'b' values
bool addition_will_overflow = INT_MAX - a < b;
bool multipliation_will_overflow = INT_MAX / a < b;

Assuming non-negative values, of course.

This approach will work but will likely be sub-optimal. From a language
lawyer perspective unsigned arithmetic would do the trick nicely for
additon, because if x+y < x, then you have an overflow, and otherwise
not (y would have to be 2^n to produce x, and you always have y<2^n).

For unsigned multiplication I don't know an efficient way to detect
overflow, within the boundaries of standard C++. But there may be some
way. There often is.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Integer Literal Question 0
64 bit design 2
64 bit integers etc... 11
32/64 bit cc differences 110
Any integer number is always 32 bits 2
64-bit KISS RNGs 23
64 bit integer 20
64-bit Python for Solaris 0

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top