Android Question xor ussage

emexes

Well-Known Member
Licensed User
Bear in mind that Bytes in B4A/B4I/B4J are signed, with values from -128 to 127. You can assign unsigned 8-bit values to them, but when you use the variable value, you'll find that the positive value you put into the variable eg 240 has changed to a negative number eg -16. Is no real problem, bit operations work just fine, but can be mildly disconcerting with math operations.

You can convert the signed byte values -128..127 to unsigned values 0..255 by using Bit.And to mask it to an Int, eg:
B4X:
Dim Signed8Bits As Byte

Signed8Bits = 123
Log(Signed8Bits)

Signed8Bits = 234
Log(Signed8Bits)    '-22

Dim Signed32Bits As Int

Signed32Bits = Signed8Bits    'still -22
Log(Signed32Bits)

Log(Bit.And(Signed32Bits, 0xFF))    'high bits 31..8 masked to 0, including sign bit 31
Log(Bit.And(Signed8Bits, 0xFF))    'this still works because B4A Bit operations are 32 bits (useful to remember for the day you use them with 64-bit Longs...)
 
Top