1

I have a 16 bit word that could be anywhere from 1 to 16 data values. They are decoded by knowing the MSB and lsb of the 16 bit word and grabbing those bits.

I'm using VB and I just don't know how to do this.

Example I have a word that is
&HA6F2
1010 0100 1111 0010

I know my data is LSB 3 to MSB 9. Bit ordering is left to right So the data is 010011

How do I get this in VB code? I want to work in bytes because after I get the packed bits then I have to do type casts on it (signed_fixed, integer, 2's complement, etc)

Thanks

1
  • 1
    &HA6F2 => 1010 0110 1111 0010 !?
    – codersl
    Jan 13, 2016 at 2:05

1 Answer 1

1

You should use mask (bitwise AND, see And keyword). And also probably bitwise-right-shift (see >> operator)

Conceptually:

1010 0100 1111 0010 '= the data
0001 1111 1100 0000 '= 1FC0 the mask
-------------------- And
0000 0100 1100 0000 '= 04C0
-------------------- >> 6
0000 0000 0001 0011 '= 0013 now your value is in the right most

In the code

Dim newData As Integer = (rawData And &H1FC0) >> 6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.