First time here? Check out the site's "greatest hits" or read a post from the archives. Feel free to leave a comment or ask a question, and consider subscribing to the latest posts via RSS or e-mail. Thanks for visiting!
« UML 2.0 Diagrams and Shape Downloads for... | Main | Ah, crap... »

I was converting some C# 2.0 code for the next Coding4Fun Some Assembly Required to VB.NET. I happened to use an automated C# to VB.NET tool to get me started.

It converted this C# code:

if( (btData - '0') <= 9)
{
   receivedChecksum = (byte)((btData - '0') << 4);
}
else
{
   receivedChecksum = (byte)((btData - 'A' + 10) << 4);
}

Into this attempt at VB.NET code.

If btData - "0" <= 9 Then
   
receivedChecksum = System.Convert.ToByte(Machine.Shift.Left((btData - "0"), 4))
Else
  
receivedChecksum = System.Convert.ToByte(Machine.Shift.Left((btData - "A" + 10), 4))
End If

Of course, Machine.Shift.Left (and .Right) doesn't exist. Looks like something that the convertor folks missed? Perhaps they forgot to implement?

At any rate, you can use the standard bit shifting << and >> operators in VB.NET 2.0 like this.

If btData - "0" <= 9 Then
   
receivedChecksum = System.Convert.ToByte((btData - "0") << 4)
Else
   
receivedChecksum = System.Convert.ToByte((btData - "A" + 10) << 4)
End If

And I continue forward...

UPDATED:

You might think that VB.NET would let you use ^= if you can << and >>.

Well, it will compile things like

foo ^= bar

But the ^= operator means Power Of in VB, not Xor as I thought it should. Doh! I'm out of VB.NET practice.

VB.NET folks, I'm sorry, but when it comes down to manipulating raw Bytes, the language sucks.

foo = foo Xor bar

And I continue forward...



Tuesday, May 24, 2005 7:48:27 AM (Pacific Standard Time, UTC-08:00)
It's there, and it does suck, but then again look at the target audience... Every time I show any kind of bit manipulation to VB students, their eyes glaze over like a Kristy Kreme donut. They can't imagine every wanting or needing to do that.
Scott
Tuesday, May 24, 2005 9:09:06 AM (Pacific Standard Time, UTC-08:00)
I dont think you need to apologize - the language was not designed for that usage and does not pretend to be. Just like C# sucks for Office interop - no apology necessary.
Tuesday, May 24, 2005 9:21:05 AM (Pacific Standard Time, UTC-08:00)
Scott, I've worked with VB for years and if I needed some more horsepower, I just went to C++. VB is a little too high level for heavy duty crunching. No apologies necessary.

(Trackback didn't work, as far as I can tell, so here's mine on yours: http://dotnothing.blogspot.com/2005/05/bit-shifting-in-vbnet.html)
Tuesday, May 24, 2005 3:19:31 PM (Pacific Standard Time, UTC-08:00)
Good article. How do they decide which features get left out of C# or VB. It seems like they just ought to implement all in both. (Other than types that VB can't handle.)

The Some Assembly Required link

http://msdn.com/coding4fun/assemblyrequired

doesn't work. It gives a 404 at microsoft.com.

Aaron
Aaron
Wednesday, May 25, 2005 12:05:49 AM (Pacific Standard Time, UTC-08:00)
I fixed the link
Scott Hanselman
Thursday, May 26, 2005 10:15:55 PM (Pacific Standard Time, UTC-08:00)
>but when it comes down to manipulating raw Bytes, the language sucks

Whatever, Hanselman! And you know what? Bytes suck!
Wednesday, June 01, 2005 5:24:07 PM (Pacific Standard Time, UTC-08:00)
>And you know what? Bytes suck!

Said from a true VB programmer:)

Eric
Comments are closed.

Contact

Sponsors

Hosting By

On this page...

Tags

Calendar

<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Archives

Google Ads