How to get a particular bit value from a byte

sups123

Member
Joined
Mar 10, 2006
Messages
14
Programming Experience
1-3
Hello

some difficulty again.
I want to read a particular bit value from a byte. can anyone suggest me how to achieve this.

Following is the code which gets byte value.
===============================
Structure typByteArray
Public B() As Byte
EndStructure

Dim tB As typByteArray
ReDim tB.B(2)

'say for example it gets the following values.
tB.B(2) = 51
tB.B(1) = 121

================================

From this byte I want to get a particular bit value.
How can I get this. Is it possible.
Reply needed as soon as possible.

Thanks
sups123
 
One of the easiest ways is to use a bit mask. For instance, if you want the third bit you use a bitwise And with a value that has only that bit set:
VB.NET:
'Create a byte with only the third bit set.
Dim mask As Byte = Convert.ToByte("00000100", 2)

If (myByte And mask) = mask Then
    'myByte had the third bit set.
 
Back
Top