开发者

How can I do a bitwise-AND operation in VB.NET?

I want to perform a bitwise-AND operation in开发者_如何学C VB.NET, taking a Short (16-bit) variable and ANDing it with '0000000011111111' (thereby retaining only the least-significant byte / 8 least-significant bits).

How can I do it?


0000000011111111 represented as a VB hex literal is &HFF (or &H00FF if you want to be explicit), and the ordinary AND operator is actually a bitwise operator. So to mask off the top byte of a Short you'd write:

shortVal = shortVal AND &HFF

For more creative ways of getting a binary constant into VB, see: VB.NET Assigning a binary constant


Use the And operator, and write the literal in hexadecimal (easy conversion from binary):

theShort = theShort And &h00ff

If what you are actually trying to do is to divide the short into bytes, there is a built in method for that:

Dim bytes As Byte() = BitConverter.GetBytes(theShort)

Now you have an array with two bytes.


result = YourVar AND cshort('0000000011111111')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜