By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,684 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,684 IT Pros & Developers. It's quick & easy.

Problem

P: n/a
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.

Nov 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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

Nov 20 '05 #2

P: n/a
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
Nov 20 '05 #3

P: n/a
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
Nov 20 '05 #4

P: n/a
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

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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


Nov 20 '05 #5

P: n/a
Cor
Hi Jay B,
That's it, clever,
LOL
Cor
Nov 20 '05 #6

P: n/a
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
Nov 20 '05 #7

P: n/a
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/pr...o/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

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl... 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

Nov 20 '05 #8

P: n/a
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
Nov 20 '05 #9

P: n/a
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 -
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.