434,636 Members | 1,945 Online
Need help? Post your question and get tips & solutions from a community of 434,636 IT Pros & Developers. It's quick & easy.

# Bitwise shift in VB.Net 2002

 P: n/a How do you do a bit shift in VB.Net 2002? In VB.Net 2003n you can use the << or >> operators. Thanks Rudolf Nov 20 '05 #1
8 Replies

 P: n/a Rudolf: VB.NET 2002 doesnt support bitwise operators. Youll have to use binary multiplication/division to get the job done. "Rudolf" wrote in message news:17****************************@phx.gbl... How do you do a bit shift in VB.Net 2002? In VB.Net 2003n you can use the << or >> operators. Thanks Rudolf Nov 20 '05 #2

 P: n/a 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 -----Original Message-----Rudolf:VB.NET 2002 doesnt support bitwise operators. Youll have to use binarymultiplication/division to get the job done."Rudolf" wrote in messagenews:17****************************@phx.gbl... How do you do a bit shift in VB.Net 2002? In VB.Net 2003n you can use the << or >> operators. Thanks Rudolf . Nov 20 '05 #3

 P: n/a 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" | Sender: "Rudolf" | Subject: Bitwise shift in VB.Net 2002 | Date: Thu, 25 Sep 2003 00:18:34 -0700 | Lines: 7 | Message-ID: <17****************************@phx.gbl> | 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 | Nov 20 '05 #4

 P: n/a 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 "Rudolf" wrote in message news:10*****************************@phx.gbl... 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-----Original Message-----Rudolf:VB.NET 2002 doesnt support bitwise operators. Youll have to use binarymultiplication/division to get the job done."Rudolf" wrote in messagenews:17****************************@phx.gbl... How do you do a bit shift in VB.Net 2002? In VB.Net 2003n you can use the << or >> operators. Thanks Rudolf . Nov 20 '05 #5

 P: n/a Hello, "Rudolf" schrieb: How do you do a bit shift in VB.Net 2002? In VB.Net 2003n you can use the << or >> operators. http://groups.google.de/groups?selm=...ampabay.rr.com -- Herfried K. Wagner MVP · VB Classic, VB.NET http://www.mvps.org/dotnet Nov 20 '05 #6

 P: n/a 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" "Rudolf" wrote in message news:17****************************@phx.gbl... : How do you do a bit shift in VB.Net 2002? : : In VB.Net 2003n you can use the << or >> operators. : : Thanks : : Rudolf Nov 20 '05 #7

 P: n/a Thanks -----Original Message-----That should do it, although I would throw in some exception handling forpossible integer overflows.Catch ex as OverflowException ' greater than 32 bit value - ack Nov 20 '05 #8

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

### This discussion thread is closed

Replies have been disabled for this discussion.