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

Simple math overflow problem (and var declared as int64 in IDE shows as Int)

Hi folks,

I've been bashing my head against this all afternoon and am now totally
baffled.

Given this (a simple routine to turn a ip address string into an decimal)

<snip>
Dim Parts(3) as string ' --> each element is an integer in the range 0-255
Dim Big as int64 = 0

Big =16777216 * cint(Parts(0))
Big = big + (65536 * cint(Parts(1)))
big = big + (256 * cint(parts(2)))
big = big + cint(parts(3))

I get arithmetic overflow errors on execution (at the first line).
Interestingly if I play around with constants then by doing

big = 16777216 * 255

The IDE reports "Constant expression not representable in type integer".
This doesn't make a whole lot of sense given that big is declared as an
int64. Values greater than 127 result in the same - clearly suggesting an
overflow somewhere, but these numbers are well within the scope of an int64
(ordinarilly a uint32 is sufficient)

Does anyone have any suggestions to stop me going mad. I'm not that
experienced VB.NET / ASP.NET, so hopefully I'm missing something really
obvious.

Thanks

Nov 18 '05 #1
4 2149
Kevin,

I'm not an expert either but I don't see any 'int64' type for Visual Basic.
Looks like you're just getting an 'Integer' which will overflow after 2**31.
Have you tried "Long"?

Nick

"Simon Devlin" <si**********@7layerforensics.com> wrote in message
news:3f***********************@newscene.com...
Hi folks,

I've been bashing my head against this all afternoon and am now totally
baffled.

Given this (a simple routine to turn a ip address string into an decimal)

<snip>
Dim Parts(3) as string ' --> each element is an integer in the range 0-255 Dim Big as int64 = 0

Big =16777216 * cint(Parts(0))
Big = big + (65536 * cint(Parts(1)))
big = big + (256 * cint(parts(2)))
big = big + cint(parts(3))

I get arithmetic overflow errors on execution (at the first line).
Interestingly if I play around with constants then by doing

big = 16777216 * 255

The IDE reports "Constant expression not representable in type integer".
This doesn't make a whole lot of sense given that big is declared as an
int64. Values greater than 127 result in the same - clearly suggesting an
overflow somewhere, but these numbers are well within the scope of an int64 (ordinarilly a uint32 is sufficient)

Does anyone have any suggestions to stop me going mad. I'm not that
experienced VB.NET / ASP.NET, so hopefully I'm missing something really
obvious.

Thanks

Nov 18 '05 #2
Use CLng() instead of CInt().

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Simon Devlin" <si**********@7layerforensics.com> wrote in message
news:3f***********************@newscene.com...
Hi folks,

I've been bashing my head against this all afternoon and am now totally
baffled.

Given this (a simple routine to turn a ip address string into an decimal)

<snip>
Dim Parts(3) as string ' --> each element is an integer in the range 0-255 Dim Big as int64 = 0

Big =16777216 * cint(Parts(0))
Big = big + (65536 * cint(Parts(1)))
big = big + (256 * cint(parts(2)))
big = big + cint(parts(3))

I get arithmetic overflow errors on execution (at the first line).
Interestingly if I play around with constants then by doing

big = 16777216 * 255

The IDE reports "Constant expression not representable in type integer".
This doesn't make a whole lot of sense given that big is declared as an
int64. Values greater than 127 result in the same - clearly suggesting an
overflow somewhere, but these numbers are well within the scope of an int64 (ordinarilly a uint32 is sufficient)

Does anyone have any suggestions to stop me going mad. I'm not that
experienced VB.NET / ASP.NET, so hopefully I'm missing something really
obvious.

Thanks

Nov 18 '05 #3
int64 is a .Net Data Type, available via C# OR VB.Net, or any other .Net
language.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Nick Savoiu" <savoiu@ics_.uci_.edu_> wrote in message
news:#3**************@tk2msftngp13.phx.gbl...
Kevin,

I'm not an expert either but I don't see any 'int64' type for Visual Basic. Looks like you're just getting an 'Integer' which will overflow after 2**31. Have you tried "Long"?

Nick

"Simon Devlin" <si**********@7layerforensics.com> wrote in message
news:3f***********************@newscene.com...
Hi folks,

I've been bashing my head against this all afternoon and am now totally
baffled.

Given this (a simple routine to turn a ip address string into an decimal)
<snip>
Dim Parts(3) as string ' --> each element is an integer in the range

0-255
Dim Big as int64 = 0

Big =16777216 * cint(Parts(0))
Big = big + (65536 * cint(Parts(1)))
big = big + (256 * cint(parts(2)))
big = big + cint(parts(3))

I get arithmetic overflow errors on execution (at the first line).
Interestingly if I play around with constants then by doing

big = 16777216 * 255

The IDE reports "Constant expression not representable in type integer".
This doesn't make a whole lot of sense given that big is declared as an
int64. Values greater than 127 result in the same - clearly suggesting an overflow somewhere, but these numbers are well within the scope of an

int64
(ordinarilly a uint32 is sufficient)

Does anyone have any suggestions to stop me going mad. I'm not that
experienced VB.NET / ASP.NET, so hopefully I'm missing something really
obvious.

Thanks


Nov 18 '05 #4
Thanks. That's done the trick, but I don't really understand why. Surely
the parenethesis should mean that the cint conversion should occur first
(and I know that the target of that operation is always a 8 bit value).

I'm also not clear why the constant expression in the IDE errors too.

I'm happy that this has resolved my problem, but it all seems a little odd.

Regards

"Kevin Spencer" <ke***@takempis.com> wrote in message
news:Ok**************@TK2MSFTNGP10.phx.gbl...
Use CLng() instead of CInt().

Nov 18 '05 #5

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

Similar topics

1
by: barnesc | last post by:
Hi! Here's a simple hashcash implementation in Python. 28 lines of actual code. Can be reduced to 17 lines for instructional purposes, if you don't want clustering, and use xrange() instead...
8
by: Tom | last post by:
Has anyone ever seen a IComparer for floats the returns magnitude. i.e. instead of returning -1, it would return -5. To let you know HOW different the two numbers are. obviously for int it is a -...
44
by: JKop | last post by:
You know how the saying goes that *unsigned* overflow is... well.. defined. That means that if you add 1 to its maximum value, then you know exactly what value it will have afterward on all...
3
by: Unknown | last post by:
Why does this overflow? long testmath = (898 * 360 + 30) * 24 * 60 * 60 * 1000 / 25; but if I do it this way it works.. long testmath1 = (898 * 360 + 30) long testmath2 = testmath1 * 24 *...
11
by: Kush | last post by:
Hello All, I am having problem writing some data to file and I do not understand my mistake / misconception. I am trying to write some simple text to file but the text is rather output to...
3
by: _Christopher\(M2M\) | last post by:
Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP.050727-7600) How do I get rid of "Warning C4307: '-' : integral constant overflow" from the following macro? #define...
1
by: jwmaiden | last post by:
I'm using VB.NET on Visual Studio 2005. Trying to write a program to do combinatorics, and I'm getting issues with overflow. When I call the function Combo I get an error message when Combo tries to...
17
by: Tarique | last post by:
This program was compiled on MS Visual C++ 08 /*Fibonacci Numbers*/ #include<stdio.h> #include<limits.h> void fibonacci(int n) { unsigned long long fib0 = 0; /*First Fibonacci Number*/
8
by: =?Utf-8?B?bWFyaw==?= | last post by:
consider: Int64 steps = 0; public Int64 recurse(double A) { steps += 1; A = A/2; try { A = recurse(A); }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.