Problem

P

Patrick

Hi,

This is actually a VB 6.0 query, not VB.NET. I posted it in VB NG but did not get a reply..hopefully I will get some help out here..

-------------
Whenever i test the follwing, an Overflow error -6 occurs

sun test()

dim L as long

dim D as double

dim result as double

L=12112 'any value

D=2^31

result= L and D

result= L or D

result= L or(not D)

end sub

It seems that the bitwise operator "or,Xor,And,Not" may not work on 32bit number with signbit? Please give some solution.
 
C

Cor

Hi Patrick or is it Gary,
The same thread is in this newsgroup.
This one of the favorite questions for Fergus,
So there are very nice answers at Gary.
Cor
 
F

Fergus Cooney

Hi Patrick,

As Cor says, this has been answered already.

If you are Gary, posting under the topic: Help!, then you know what I'm
talking about.

If you are Patrick, a different person to Gary, but with exactly the same
query!!, do a search for 2^31 and you will find the other question and the
answers.

Regards,
Fergus
 
F

Fergus Cooney

Hi again,

Again, if you are Gary, you posted a query about serialization, topic:
Advice needed !. That has also been answered. I have added a footnote
containing 2^31 so that the same search will find it.

;-)

As an aside:
Almost all the queries we get are from people with a 'problem' who
need some 'help' and 'advice' - whatever those words might mean at the time.
Can I recommend that you use titles that convey some information to us
as query responders and also to those who search the newsgroups for topics
that might help them?

Regards,
Fergus
 
J

Jay B. Harlow [MVP - Outlook]

Fergus,
I suspect its the same homework assignment! ;-)

Of course with VB.NET 2003 Gary Patrick can use the new bit shift operator &
safe the expense of the exponent & the doubles!

Sub Test()
Dim L As Integer
Dim D As Integer
Dim result As Integer

L = 12112 'any value
D = 1 << 31
result = L And D
result = L Or D
result = L Or (Not D)
End Sub

Which BTW works correctly on the sign bit!

Jay
 
F

Fergus Cooney

Hi Jay,

Now <that's> handy - I hadn't discovered (or registered, perhaps)
bit-shifting in VB.NET.

Finally! some attention to bits and bytes. Now all we need is an unsigned
type, perhaps, and a bit more work on casting Longs with 'Int' sign-bits.

Regards,
Fergus
 
J

Jay B. Harlow [MVP - Outlook]

Fergus,
Just note, you need VB.NET 2003 (.NET 1.1) for bit shifting.

VB.NET 2004 (.NET 2.0) we are suppose to get unsigned types.

http://msdn.microsoft.com/vstudio/productinfo/roadmap.aspx
perhaps, and a bit more work on casting Longs with 'Int' sign-bits.
Are you saying you want:

Dim l As Long = 1L << 31
Dim i As Integer = CInt(l)

Where you get a negative number instead of overflow?

You can use System.BitConverter:

Dim l As Long = 1L << 31
Dim i As Integer

Dim bytes() as Byte
bytes() = BitConverter.GetBytes(l)
i = BitConverter.ToInt32(bytes, 0)

There may be a 'more efficient' way, that I'm not thinking of, but the above
works ;-)

Of course the above effectively truncates the high order half of the long.

Or are you thinking something else?

Hope this helps
Jay
 
F

Fergus Cooney

Hi Jay,

Ha!. When I read about VB bit-shifts in your post I thought I must be
getting tired. I've spent the weekend converting a calculator from QBasic (of
all things) to .NET so I was studying operator precedence and expression
grammar. Never saw bit shifts. LOL. Of course I hadn't discovered them - I've
got v1.0 and the corresponding Help.

!! ... negative number instead of overflow?

Yes, that's the chap. In days gone past it could be done using Peek and
Poke, LSet-ing a string into a random-access file's buffer and reading an Int
out, and all sorts. Thanks for the BitConverter - that's the VB.NET
workaround, eh? Lol. It beats doing Long to bytes, bytes to string (Unicode
BigEndian), string to bytes (LittleEndian), bytes to Int (I know,.. joke).

I'd read the C# roadmap but not the VB one. I look forward to operator
overloading - every class should have at least one!! (I know,.. joke).
Partials and generics will be good, though. Roll on 2004 and unsigned types. I
hope they're more useful that the C# ones which seem to need more casting than
a war movie.

Cheers,
Fergus
 
G

Gary

Hi Fergus,

Thanks for the reply.

I am sorry for using such *meaningless* subjects, your point taken ... yes I
need to provide some info. in the subject line so that
people can get some idea about the problem.

Thanks all, for all your wonderful replies :)

- Gary -
 

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

Top