I'm trying to do some bit operations (esp. OR) on bytes and get some very strange results. I boiled it down to:
B4X:
Dim foo as byte = 0
foo = Bit.OR (foo, Bit.ShiftLeft(1, 7))
Bit.ToBinaryString (foo) 'I would except to get 10000000,but I get 1111111111111111111111110000000 ?
I guess it has something to do with the fact the Bit operators return ints, but still I don't get it ?
foo is a signed byte variable with a range of -128 to 127. 10000000 is -128. The value of foo is passed as an integer value to Bit.ToBinaryString. 11111111111111111111111110000000 is -128.
You can convert foo to an unsigned byte and return it as an integer with this: