Hi all,
I have a very basic (most probably stupid..) question.
I am using the bit object to manipulate the bits inside a byte that I must then write to a sensor (which has a memory byte-organized).
I am using the below sub to set/reset the bit within my byte:
Here the problem. If I run the below code
I would expect b0 to be:
10000001 (129 or 0x81)
after all b0 is a byte..
but what I get is:
11111111111111111111111110000001
Now. If I do the same with bit 0 and bit 6, everything works as expected and b0 stays byte..
It looks to me that, since the Bit.Or and Bit.Not,etc. work (return and get as input) with "int", they "cast" my byte into a INT (32 bit or 4 bytes).
Any idea?
thank you very much.
I have a very basic (most probably stupid..) question.
I am using the bit object to manipulate the bits inside a byte that I must then write to a sensor (which has a memory byte-organized).
I am using the below sub to set/reset the bit within my byte:
B4X:
Sub SetBit(b As Byte, index As Int, value As Boolean) As Byte
If value Then
Return Bit.Or(b, Bit.ShiftLeft(1, index))
Else
Return Bit.And(b, Bit.Not(Bit.ShiftLeft(1, index)))
End If
End Sub
Here the problem. If I run the below code
B4X:
dim b0 as byte
b0=SetBit(b0,0,True)
b0=SetBit(b0,7,True)
Log(Bit.ToBinaryString(b0))
I would expect b0 to be:
10000001 (129 or 0x81)
after all b0 is a byte..
but what I get is:
11111111111111111111111110000001
Now. If I do the same with bit 0 and bit 6, everything works as expected and b0 stays byte..
It looks to me that, since the Bit.Or and Bit.Not,etc. work (return and get as input) with "int", they "cast" my byte into a INT (32 bit or 4 bytes).
Any idea?
thank you very much.