Bitwise shift in VB.Net 2002

  • Thread starter Thread starter Rudolf
  • Start date Start date
R

Rudolf

How do you do a bit shift in VB.Net 2002?

In VB.Net 2003n you can use the << or >> operators.

Thanks

Rudolf
 
Rudolf:

VB.NET 2002 doesnt support bitwise operators. Youll have to use binary
multiplication/division to get the job done.
 
Thanks,

I eventually ended up using


Private Function ShiftL(ByVal val As Integer, ByVal bits
As Integer) As Integer
If bits = 0 Then Return 0
Return CType(val / (2 ^ bits), Integer)
End Function

Rudolf
 
Hi Rudolf,

There isn't a bitwise shift in VB.Net 2002. I think you can write your own
function to achieve this. Right shift 1 bit means divided by 2, while left
shift 1 bit means multiple by 2.

Hope this helps.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| Content-Class: urn:content-classes:message
| From: "Rudolf" <[email protected]>
| Sender: "Rudolf" <[email protected]>
| Subject: Bitwise shift in VB.Net 2002
| Date: Thu, 25 Sep 2003 00:18:34 -0700
| Lines: 7
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcODNTsnTE6O9BfXT1m1j0pcFAj32Q==
| Newsgroups: microsoft.public.dotnet.languages.vb
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:141046
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| How do you do a bit shift in VB.Net 2002?
|
| In VB.Net 2003n you can use the << or >> operators.
|
| Thanks
|
| Rudolf
|
 
That should do it, although I would throw in some exception handling for
possible integer overflows.

Catch ex as OverflowException
' greater than 32 bit value - ack
 
Hi Rudolf,

Shift to the left:

Result = Number * (2 ^ (number of bits))

Shift to the right:

Result = Number \ (2 ^ (number of bits))

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Chaos, Panic, Disorder, my work here is done"


: How do you do a bit shift in VB.Net 2002?
:
: In VB.Net 2003n you can use the << or >> operators.
:
: Thanks
:
: Rudolf
 
Hi Rudolf,

There's an important but very unobvious flaw in your routine!!

Private Function ShiftL_RealDiv (ByVal val As Integer, _
ByVal bits As Integer) As Integer
If bits = 0 Then Return 0
Return CType (val / (2 ^ bits), Integer)
End Function
vs
Private Function ShiftL_IntDiv (ByVal val As Integer, _
ByVal bits As Integer) As Integer
If bits = 0 Then Return 0
Return val \ CInt (2 ^ bits)
End Function

This is the output of a loop doing ShiftL (I, 1):

I = 0, ShiftL_Int = 0, ShiftL_Float = 0
I = 1, ShiftL_Int = 0, ShiftL_Float = 0
I = 2, ShiftL_Int = 1, ShiftL_Float = 1
I = 3, ShiftL_Int = 1, ShiftL_Float = 2 x
I = 4, ShiftL_Int = 2, ShiftL_Float = 2
I = 5, ShiftL_Int = 2, ShiftL_Float = 2
I = 6, ShiftL_Int = 3, ShiftL_Float = 3
I = 7, ShiftL_Int = 3, ShiftL_Float = 4 x
I = 8, ShiftL_Int = 4, ShiftL_Float = 4
I = 9, ShiftL_Int = 4, ShiftL_Float = 4
I = 10, ShiftL_Int = 5, ShiftL_Float = 5
I = 11, ShiftL_Int = 5, ShiftL_Float = 6 x
I = 12, ShiftL_Int = 6, ShiftL_Float = 6
I = 13, ShiftL_Int = 6, ShiftL_Float = 6
I = 14, ShiftL_Int = 7, ShiftL_Float = 7
I = 15, ShiftL_Int = 7, ShiftL_Float = 8 x

As you can see the integer division has the expected pairs while the
floating division produces singlets and triples.

The difference is due to the way that VB now rounds floating point
division to the nearest even number (round-to-even). It's to help with
accuracy in floating point arithmetic but it really screws things up when
doing integer arithmetic. It's one that you have to watch out for as it's a
real hidden bug producer. $%£&*!! on the person who decided that this was the
way to go.

Regards,
Fergus

ps. If bits = 0 Then Return 0
Why do you return 0? A shift of 0 bits should return the original number,
surely?
 
Back
Top