Home All Groups Group Topic Archive Search About

newbie question vb.net bits

Author
13 Jan 2006 2:59 PM
marfi95
If I have a 2 byte byte array, how do I test bits within the bytes to
determine if they are on ?

for example, I need to check if bit 0 and 6 are on in byte 1  and bit
0, 2 and 5 in byte 2.

I think I "and" it with 0x01 and 0x0x40, but am not familiar with the
syntax or how to get to this level.  Thanks.   Also, do you check for
each one, or is there one value you use ?  I problably need to do it
both ways in another section.

thanks.
mark

Author
13 Jan 2006 6:14 PM
_AnonCoward
<marf***@yahoo.com> wrote in message
Show quoteHide quote
news:1137164357.648243.234830@o13g2000cwo.googlegroups.com...
:
: If I have a 2 byte byte array, how do I test bits within the bytes to
: determine if they are on ?
:
: for example, I need to check if bit 0 and 6 are on in byte 1  and bit
: 0, 2 and 5 in byte 2.
:
: I think I "and" it with 0x01 and 0x0x40, but am not familiar with the
: syntax or how to get to this level.  Thanks.   Also, do you check for
: each one, or is there one value you use ?  I problably need to do it
: both ways in another section.
:
: thanks.
: mark


Allow me to offer an example as an explanation (note: I'm counting bit
positions from right to left starting with 0 consistent with your example)

'-------------------------------------
Dim TestByte As Byte
Dim BitTest As Byte
Dim BitValue As Byte
Dim BitSet As Boolean

'Byte 5 = binary value 0000 0101
TestByte = 5

'We want to test to see if bits 1, 2, 3, or 4 are set
For bitPosition As Byte = 0 To 3

  'First, we convert this to a power of 2
  '2^0 = 1 = 0001
  '2^1 = 2 = 0010
  '2^2 = 4 = 0100
  '2^3 = 8 = 1000
  BitTest = CByte(2^bitPosition)

  'See if the bit is set
  BitValue = TestByte And BitTest

  If BitValue > 0 Then
    BitSet = True
  Else
    BitSet = False
  End If

  'Output the result
  Console.WriteLine("Bit position " & BitPosition & _
                    " for Byte Value " & TestByte & _
                    " is 'On': " & BitSet)

Next

'-------------------------------------


The key statement here is BitValue = TestByte And BitTest. You can define
BitTest however you want. For example, you can use the power of 2 approach
above or you can define it directly. Whatever.


Also, this example tests for one bit at a time. If you wanted to test for
several bits all at once, you can. Just use a value with the bits you want
tested for turn on and see if the resulting And operation returns that
value. For example, if you wanted to know if the first two bits are set, you
could use

  BitValue = TestByte And 3
  If BitValue = 3 Then
    BitSet = True
  Else
    BitSet = False
  End If

HTH

Ralf
--
--
----------------------------------------------------------
*             ^~^                   ^~^                  *
*          _ {~ ~}                 {~ ~} _               *
*         /_``>*<                   >*<''_\              *
*        (\--_)++)                 (++(_--/)             *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Author
13 Jan 2006 6:47 PM
marfi95
Thanks !