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

Long Arithmetic not elegant in .NET

This doesn't work:

Dim t As DateTime = DateTime.Now
Dim a As Long = t.Ticks

Dim t2 As DateTime = t.AddSeconds(1)
Dim b As Long = t2.Ticks

Dim ans As Long = b - a

sets ans to 0 instead of 1 million. You have to do this:

Dim t As DateTime = DateTime.Now
Dim a As Long = t.Ticks

Dim t2 As DateTime = t.AddSeconds(1)
Dim b As Long = t2.Ticks

Dim ans As Long = (DirectCast(b, Long) - DirectCast(a, Long))

to get around it. Although is there a more elegant way?

Phill
Mar 3 '08 #1
6 1235
On Mar 3, 6:20 pm, Phillip Taylor <Phillip.Ross.Tay...@gmail.com>
wrote:
This doesn't work:

Dim t As DateTime = DateTime.Now
Dim a As Long = t.Ticks

Dim t2 As DateTime = t.AddSeconds(1)
Dim b As Long = t2.Ticks

Dim ans As Long = b - a

sets ans to 0 instead of 1 million. You have to do this:

Dim t As DateTime = DateTime.Now
Dim a As Long = t.Ticks

Dim t2 As DateTime = t.AddSeconds(1)
Dim b As Long = t2.Ticks

Dim ans As Long = (DirectCast(b, Long) - DirectCast(a, Long))

to get around it. Although is there a more elegant way?

Phill
instead of ten million*
Mar 3 '08 #2
The first code snippet works fines here ??? (.NET 2.0). How do you see the
value for ans ?

--
Patrice

"Phillip Taylor" <Ph*****************@gmail.coma écrit dans le message de
news: b7**********************************...oglegroups.com...
This doesn't work:

Dim t As DateTime = DateTime.Now
Dim a As Long = t.Ticks

Dim t2 As DateTime = t.AddSeconds(1)
Dim b As Long = t2.Ticks

Dim ans As Long = b - a

sets ans to 0 instead of 1 million. You have to do this:

Dim t As DateTime = DateTime.Now
Dim a As Long = t.Ticks

Dim t2 As DateTime = t.AddSeconds(1)
Dim b As Long = t2.Ticks

Dim ans As Long = (DirectCast(b, Long) - DirectCast(a, Long))

to get around it. Although is there a more elegant way?

Phill

Mar 3 '08 #3
On Mar 3, 8:20 pm, Phillip Taylor <Phillip.Ross.Tay...@gmail.com>
wrote:
This doesn't work:

Dim t As DateTime = DateTime.Now
Dim a As Long = t.Ticks

Dim t2 As DateTime = t.AddSeconds(1)
Dim b As Long = t2.Ticks

Dim ans As Long = b - a

sets ans to 0 instead of 1 million. You have to do this:

Dim t As DateTime = DateTime.Now
Dim a As Long = t.Ticks

Dim t2 As DateTime = t.AddSeconds(1)
Dim b As Long = t2.Ticks

Dim ans As Long = (DirectCast(b, Long) - DirectCast(a, Long))

to get around it. Although is there a more elegant way?

Phill
Tried "Dim ans As Long = b - a" and "Dim ans As Long = (DirectCast(b,
Long) - DirectCast(a, Long))" and they both outputs the same: 10000000

Couldn't notice a difference.
Mar 3 '08 #4
Works fine here, with option strict both on and off.

Tom Dacon
Dacon Software Consulting

"Phillip Taylor" <Ph*****************@gmail.comwrote in message
news:b7**********************************@e31g2000 hse.googlegroups.com...
This doesn't work:

Dim t As DateTime = DateTime.Now
Dim a As Long = t.Ticks

Dim t2 As DateTime = t.AddSeconds(1)
Dim b As Long = t2.Ticks

Dim ans As Long = b - a

sets ans to 0 instead of 1 million. You have to do this:

Dim t As DateTime = DateTime.Now
Dim a As Long = t.Ticks

Dim t2 As DateTime = t.AddSeconds(1)
Dim b As Long = t2.Ticks

Dim ans As Long = (DirectCast(b, Long) - DirectCast(a, Long))

to get around it. Although is there a more elegant way?

Phill

Mar 3 '08 #5
"Phillip Taylor" <Ph*****************@gmail.comschrieb:
This doesn't work:

Dim t As DateTime = DateTime.Now
Dim a As Long = t.Ticks

Dim t2 As DateTime = t.AddSeconds(1)
Dim b As Long = t2.Ticks

Dim ans As Long = b - a

sets ans to 0 instead of 1 million. You have to do this:

Dim t As DateTime = DateTime.Now
Dim a As Long = t.Ticks

Dim t2 As DateTime = t.AddSeconds(1)
Dim b As Long = t2.Ticks

Dim ans As Long = (DirectCast(b, Long) - DirectCast(a, Long))

to get around it. Although is there a more elegant way?
Both code samples produce the same result.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Mar 4 '08 #6
On Mar 4, 2:09 am, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrote:
"Phillip Taylor" <Phillip.Ross.Tay...@gmail.comschrieb:
This doesn't work:
Dim t As DateTime = DateTime.Now
Dim a As Long = t.Ticks
Dim t2 As DateTime = t.AddSeconds(1)
Dim b As Long = t2.Ticks
Dim ans As Long = b - a
sets ans to 0 instead of 1 million. You have to do this:
Dim t As DateTime = DateTime.Now
Dim a As Long = t.Ticks
Dim t2 As DateTime = t.AddSeconds(1)
Dim b As Long = t2.Ticks
Dim ans As Long = (DirectCast(b, Long) - DirectCast(a, Long))
to get around it. Although is there a more elegant way?

Both code samples produce the same result.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Hmmm I am unable to replicate this bug today...

:-s
Mar 4 '08 #7

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

Similar topics

83
by: kartik | last post by:
there seems to be a serious problem with allowing numbers to grow in a nearly unbounded manner, as int/long unification does: it hides bugs. most of the time, i expect my numbers to be small. 2**31...
9
by: w3r3w0lf | last post by:
hello! I have a following situation: I have a byte array where at a certain location are stored 4 bytes, and these should be "put" into long variable (or any other 4 byte one). ie: byte...
7
by: Master of C++ | last post by:
Hello Folks, I have a programming dilemma with templates and "genericity". I can think of a few solutions, but I am not sure which one is the best in terms of genetic C++ style. Any suggestions...
44
by: RB | last post by:
How to extract bytes from long, starting from the last byte? For example, I have a long number: 0x12345678 I need to represent it as the following bytes list: 0x78, 0x56, 0x34, 0x12 Thanks in...
12
by: Peter Ammon | last post by:
When I add an unsigned long long and an int, what type do each of the values get promoted to before the addition is performed? What is the type of the resulting expression? What occurs if the...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
15
by: David Geering | last post by:
Is there any standard as to what a long and int mean or is it compiler dependent? Or furthermore architecture dependent? I know a short short long int isn't actually a recognised type, but if you...
10
by: krunalb | last post by:
Hi, I am trying to shift unsigned long long value by 64 bits and this is what i get #include <stdio.h> int main() { unsigned short shiftby= 64;
28
by: Bartc | last post by:
From an article about implementing a C99 to C90 translator... How does someone use integer arithmetic of at least 64 bits, and write in pure C90? As I understand C90 (the standard is very...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
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...

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.