How do I get the bits out of a byte

S

solex

See my post below titled: "Data string with bits and words" or you can use
boolean operators to determine with bits are on for instance:

If (myByteVar And 1) Then
' first bit is on
ElseIf (myByteVar And 2) Then
' second bit is on
ElseIf (myByteVar And 4) Then
' third bit is on
ElseIf (myByteVar And 8) Then
' you get the idea
ElseIf (myByteVar And 16) Then
ElseIf (myByteVar And 32) Then
ElseIf (myByteVar And 64) Then
ElseIf (myByteVar And 128) Then
' eighth bit is on
End If
 
J

John Nurick

Hi Ken

If (x And 1) = 1 Then Debug.Print "Bit 0 is set"
....
If (x And 128) = 128 Then Debug.Print "Bit 7 is set"
 
T

Tim Ferguson

how do I extract the value of each individual bit?

public function GetBit( _
BitNumber as integer,
Value as Long) _
as Boolean

' note that BitNumber runs from 0 up to 15, and
' zero is the least significant bit
'
GetBit = CBool(Value And (2^BitNumber))

End Function

Hope that helps



Tim F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top