How do I get the bits out of a byte

  • Thread starter Thread starter Ken
  • Start date Start date
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
 
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"
 
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
 
Back
Top