Call me old fashioned, but I like my bytes to go from 0-255 because you can't send negative numbers out of the serial port unless they are strings, which kind of defeats the point.
I need to set bits in an unsigned byte array and I'm struggling a bit. Could someone please show an example of how this is done?
I've got two loops, the first counts through the byte array, the second inside that one counts through the bits to set.
I've tried using a combination of these but keep coming up with values I'm not expecting.
I think this is because even though I'm using the Unsigned function first, it's still then treating it in the SetBit function as signed.
Thanks
I need to set bits in an unsigned byte array and I'm struggling a bit. Could someone please show an example of how this is done?
I've got two loops, the first counts through the byte array, the second inside that one counts through the bits to set.
I've tried using a combination of these but keep coming up with values I'm not expecting.
B4X:
Sub Unisigned(b As Byte) As Int
Return Bit.And(0xFF, b)
End Sub
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
I think this is because even though I'm using the Unsigned function first, it's still then treating it in the SetBit function as signed.
Thanks