[英]How can I do a bitwise-AND operation in VB.NET?
I want to perform a bitwise-AND operation in VB.NET, taking a Short (16-bit) variable and ANDing it with '0000000011111111' (thereby retaining only the least-significant byte / 8 least-significant bits). 我想在VB.NET中执行按位与运算,取一个短(16位)变量并将其与'0000000011111111'进行运算(从而仅保留最低有效字节/ 8个最低有效位)。
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 . 0000000011111111表示为VB十六进制文字是&HFF(或H00FF如果你想明确),和普通AND运算实际上是位运算符 。 So to mask off the top byte of a Short you'd write: 所以要掩盖你写的Short的顶部字节:
shortVal = shortVal AND &HFF
For more creative ways of getting a binary constant into VB, see: VB.NET Assigning a binary constant 有关将二进制常量输入VB的更有创意的方法,请参阅: VB.NET分配二进制常量
Use the And
operator, and write the literal in hexadecimal (easy conversion from binary): 使用And
运算符,并以十六进制编写文字(从二进制文件轻松转换):
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: 如果你真正想要做的是将short分成字节,那么有一个内置的方法 :
Dim bytes As Byte() = BitConverter.GetBytes(theShort)
Now you have an array with two bytes. 现在你有一个包含两个字节的数组。
result = YourVar AND cshort('0000000011111111')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.