Android Question Math

red30

Active Member
Licensed User
Hi!
The received array consists of 8 bytes: Buffer(1),Buffer(2), etc. I need to work with each bit of each byte separatly ( f.e. I need to get the 5th bit of the Buffer(1)). How can I do it?
How to take the integer part of the division?
Thank you for the responce.
 

Erel

Administrator
Staff member
Licensed User
B4X:
For i = 0 To 7
   Log(GetBit(15, i))
Next

Sub SetBit(b As Byte, index As Int, on As Boolean) As Byte
  If on Then
   Return Bit.Or(b, Bit.ShiftLeft(1, index))
  Else
   Return Bit.And(b, Bit.Not(Bit.ShiftLeft(1, index)))
  End If
End Sub

Sub GetBit(b As Byte, index As Int) As Boolean
   Dim t As Byte = Bit.ShiftLeft(1, index)
   Return Bit.And(b, t) = t
End Sub
 
Top