1

鉴于此短(签名):

&Hxxxx

我想要:

  • 提取最右边&HxxFF的 SByte (signed)
  • 将左边提取&H7Fxx为字节(无符号)
  • 确定最左边&H8xxx的是正数还是负数(布尔结果)
4

5 回答 5

1
inline char getLsb(short s)
{
    return s & 0xff;
}

inline char getMsb(short s)
{
    return (s & 0xff00) >> 8; 
}

inline bool isBitSet(short s, unsigned pos)
{
    return (s & (1 << pos)) > 0;
}
于 2010-07-07T23:07:21.387 回答
1

提取最右边的0xxxff

myShort & 0x00FF

提取左边的0xffxx

(myShort & 0xFF00) >> 8

确定最左边的 0xfxxx 是正数还是负数(它是一个有符号短)。

(myShort & 0xF000) >= 0;
于 2010-07-07T23:16:42.227 回答
1
Dim test As UInt16 = &HD 'a test value 1101
Dim rb As Byte 'lsb
Dim lb As Byte 'msb - 7 bits
Dim rm As UInt16 = &HFF 'lsb mask
Dim lm As UInt16 = &H7F00   'msb mask
Dim sgn As Byte = &H80 'sign mask

For x As Integer = 0 To 15 'shift the test value one bit at a time
    rb = CByte(test And rm) 'get lsb
    lb = CByte((test And lm) >> 8) 'get msb
    Dim lbS, rbS As Boolean 'sign

    'set signs
    If (rb And sgn) = sgn Then rbS = True _
     Else rbS = False

    If (lb And sgn) = sgn Then lbS = True _
     Else lbS = False 'should always be false based on mask

    Console.WriteLine(String.Format("{0} {1} {2}  {3} {4}",
        x.ToString.PadLeft(2, " "c),
        Convert.ToString(lb, 2).PadLeft(8, "0"c),
        Convert.ToString(rb, 2).PadLeft(8, "0"c),
        lbS.ToString, rbS.ToString))

    test = test << 1
Next
于 2010-07-08T00:58:18.733 回答
0

呃...

value & 0x00ff
(value & 0xff00) >> 8
(value & 0xf000) >= 0

编辑:我想你想要字节值而不仅仅是高 8 位。

于 2010-07-07T23:16:55.260 回答
0
  • 提取最右边的 &HxxFF 作为 SByte (signed)

    CType(s AND &H00FF, SByte)

  • 将左边的 &H7Fxx 提取为字节(无符号)

    CType((s AND &H7F00) >> 8, Byte)

  • 识别最左边的 &H8xxx 是正数还是负数(bool 结果)

    s AND &H8000 > 0

我认为这些工作,自从我在 VB 工作以来已经有一段时间了

于 2010-07-07T23:57:42.763 回答