开发者

vb.net - hex, bitwise question

开发者 https://www.devze.com 2023-02-22 22:10 出处:网络
I\'m trying to figure out how to calculate the lower 7 bits and 7-13 bits of two hex numbers. Here is some example c code, just need this in vb.net:

I'm trying to figure out how to calculate the lower 7 bits and 7-13 bits of two hex numbers.

Here is some example c code, just need this in vb.net:

serialBytes[2] = 0x64 & 0x7F; 开发者_开发百科// Second byte holds the lower 7 bits of target.
serialBytes[3] = (0x64 >> 7) & 0x7F;   // Third data byte holds the bits 7-13 of target

The 0x7F is a constant so the only number that changes based off input is the 0x64.

Can someone help me out?


The code translates into this VB code:

serialBytes(2) = &h64 And &h7F
serialBytes(3) = (&h64 >> 7) And &h7F

If the hex value 64 is actually a variable input, just replace &h64 with the input variable. If the input is an integer, you have to cast the results to byte, though:

serialBytes(2) = CType(value And &H7F, Byte)
serialBytes(3) = CType((value >> 7) And &H7F, Byte)


VB.NET has no bit shift operators, but does have the bitwise operator And:

set bits1to7  = value And &H007F
set bits8to14 = value And &H3F80

Edit:

VB.NET does have bit shift operators (my bad) since .NET Framework 1.1, so the more systematic approach is indeed also possible:

set bits1to7  = value And &H7F
set bits8to14 = (value >> 7) And &H7F
0

精彩评论

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