Bug? AND 0xFF - 255 failure!!!

MarcelloCSI

Active Member
Licensed User
Goodmorning everyone is reading,

I'm writing here to let you know of a possible bug (i don't know really if it is a bug).

I was writing some code that works on byte conversions and i noticed an issue:

Every time i call this function i'm obtaining 0 (zero) as result of the function.
B4X:
Sub Byte2Unsigned(B As Byte) As Byte  
    Return Bit.AND(B, 0xFF)
End Sub
In this way the function is working perfectly!
B4X:
Sub Byte2Unsigned(B As Byte) As Byte   
    Return Bit.AND(B, 255)
End Sub
Erel can you explain me why the function isn't working with the 0xFF?

Thank in advance,
Marcello Quaglia
 

MarcelloCSI

Active Member
Licensed User
Case 1:

B4X:
Return Bit.AND(B, 0xFF)
End sub....

Sub mySub...
Log(Args.Get(0) & " - " & NumFnc.Byte2Unsigned(Args.Get(0)))
End sub
the result of the log is '136 - 0'


Case 2:

B4X:
Return Bit.AND(B, 255)
End sub....

Sub mySub...
Log(Args.Get(0) & " - " & NumFnc.Byte2Unsigned(Args.Get(0)))
End sub
the result of the log is '136 - 136'

Problem is the mask of bit.AND function, i can't get correct result if use hexadecimal mask (0xFF), i get correct result if ude decimal mask (255)...
Hope you understand
 

sorex

Expert
Licensed User
did you try with 0x0ff or 0x00ff aswell?

it might treat 0xff as a negative value (-127) since the 7th bit is set. the extra 0's might shift the negative bit
 
Top