views:

70

answers:

5

Given this Short (signed):

&Hxxxx

I want to:

  • Extract the most right &HxxFF as SByte (signed)
  • Extract the left &H7Fxx as Byte (unsigned)
  • Identify if the most left &H8xxx is positive or negative (bool result)
A: 

( 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;
}
Steven Jackson
A: 

Extract the most right 0xxxff

myShort & 0x00FF

Extract the left 0xffxx

(myShort & 0xFF00) >> 8

Identify if the most left 0xfxxx is positive or negative (it's a signed short).

(myShort & 0xF000) >= 0;
Justin Ardini
A: 

Uh...

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

EDIT: I suppose you want the byte value and not just the upper 8 bits.

MSN
A: 
  • Extract the most right &HxxFF as SByte (signed)

    CType(s AND &H00FF, SByte)

  • Extract the left &H7Fxx as Byte (unsigned)

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

  • Identify if the most left &H8xxx is positive or negative (bool result)

    s AND &H8000 > 0

I think those work, been a while since I have worked in VB

Guvante
+1  A: 
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
dbasnett
@Shimmy - console in a form application? that is why I use Debug, it doesn't matter if it is form or console.
dbasnett
nevermind, i also formatted the code to suit vb 10 if you notice which is also the reason i edited
Shimmy
I wrote it in VB10 and it worked just fine.
dbasnett