Machine.Shift.Left and Bit Shifting in VB.NET
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 ThenreceivedChecksum = 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 ThenreceivedChecksum = 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...
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
(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)
The Some Assembly Required link
http://msdn.com/coding4fun/assemblyrequired
doesn't work. It gives a 404 at microsoft.com.
Aaron
Whatever, Hanselman! And you know what? Bytes suck!
Comments are closed.