473,394 Members | 1,916 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,394 software developers and data experts.

How slow the 64 bit integer calculation?

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.

Mar 4 '06 #1
3 3002
li*****@hotmail.com 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.

Mar 4 '06 #2
li*****@hotmail.com wrote:
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
--
Please remove capital As from my address when replying by mail
Mar 4 '06 #3
* Victor Bazarov:
li*****@hotmail.com wrote:
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;


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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 4 '06 #4

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

Similar topics

9
by: drife | last post by:
Hello, I need to calculate the eigenvectors and eigenvalues for a 3600 X 3600 covariance matrix. The LinearAlgebra package in Python is incredibly slow to perform the above calculations...
1
by: John Black | last post by:
Hi, If I have many integer calculation in my code, what's the best way to detect integer overflow? unsigned int i1 = 0xFFFFFF00, i2 = 0xFFFF; then in statement unsigned int i3 = i1 + i2;...
2
by: ThurstonHowl | last post by:
Hello, my task is the following: Input are tables with fields containing strings where the strings are actually delimited lists. For example, one field could contain 'AB|CD|EF|GH' I've...
11
by: Andy_Khosravi | last post by:
My problem: I'm having trouble with a query taking much too long to run; a query without any criteria evaluating only 650 records takes over 300 seconds to run (over the network. On local drive...
3
by: michael | last post by:
Hi, I am trying to write an ASP.NET web app, in Visual Basic.NET, using Visual Studio.NET 2004, .NET framework 1.1.4322 SP1 Running the project/app on localhost while in dev/write/debug stage ...
14
by: teslar91 | last post by:
As a fairly new .NET coder, I would greatly appreciate some comments on any .NET classes that are known to be notoriously slow by comparison to direct API calls. I've already had a bad...
18
by: Alex Vinokur | last post by:
Ho to get valid value of integer part of double _as int value_? Here is a program that demonstrate the problem. ------ foo.c ------ #include <stdio.h> #include <math.h> int main () {
1
by: erick-flores | last post by:
Hello all I have a linked table using ODBC. When I tried to do Count() and Sum() for this linked table (onw of the field in the table is name "total") the form takes forever to gives me the...
6
by: anders.johansen | last post by:
Hi, I have a tight loop where I basically do this over and over (millions of times): int cell = <calculation> const int above = <calculation> if (cell above) cell = above; const int below =...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.